Debian Codegraph @

converter/other/fiasco/codec/domain-pool.c

raw
1 /*
2 * domain-pool.c: Domain pool management (probability model)
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 #include <stdlib.h>
21 #include <string.h>
22
23 #include "pm_c_util.h"
24
25 #include "types.h"
26 #include "macros.h"
27 #include "error.h"
28
29 #include "misc.h"
30 #include "cwfa.h"
31 #include "wfalib.h"
32 #include "domain-pool.h"
33
34 /*
35 * Domain pool model interface:
36 * Implementing the domain pool model interface requires the
37 * following steps:
38 * - Add a constructor that initializes the domain_pool_t structure
39 * - Allocate new model with default_alloc()
40 * - Fill the dp_array_t domain_pools array with constructor and name
41 * - Write code for methods bits() and generate()
42 * - Either use default functions for remaining methods or override them
43 * The new model is automatically registered at the command line.
44 */
45
46 /*****************************************************************************
47
48 local variables
49
50 *****************************************************************************/
51
52 static real_t *matrix_0 = NULL;
53 static real_t *matrix_1 = NULL;
54
55 /*****************************************************************************
56 non-adaptive domain pool
57 *****************************************************************************/
58
59 static void
60 qac_chroma (unsigned max_domains, const wfa_t *wfa, void *model);
61 static bool_t
62 qac_append (unsigned new_state, unsigned level, const wfa_t *wfa, void *model);
63 static void
64 qac_update (const word_t *domains, const word_t *used_domains,
65 unsigned level, int y_state, const wfa_t *wfa, void *model);
66 static real_t
67 qac_bits (const word_t *domains, const word_t *used_domains,
68 unsigned level, int y_state, const wfa_t *wfa, const void *model);
69 static word_t *
70 qac_generate (unsigned level, int y_state, const wfa_t *wfa,
71 const void *model);
72 static void *
73 qac_model_duplicate (const void *src);
74 static void
75 qac_model_free (void *model);
76 static void *
77 qac_model_alloc (unsigned max_domains);
78 static domain_pool_t *
79 alloc_qac_domain_pool (unsigned max_domains, unsigned max_edges,
80 const wfa_t *wfa);
81
82 /*****************************************************************************
83 run length encoding pool
84 *****************************************************************************/
85
86 static domain_pool_t *
87 alloc_rle_no_chroma_domain_pool (unsigned max_domains, unsigned max_edges,
88 const wfa_t *wfa);
89 static void
90 rle_chroma (unsigned max_domains, const wfa_t *wfa, void *model);
91 static bool_t
92 rle_append (unsigned new_state, unsigned level, const wfa_t *wfa, void *model);
93 static void
94 rle_update (const word_t *domains, const word_t *used_domains,
95 unsigned level, int y_state, const wfa_t *wfa, void *model);
96 static real_t
97 rle_bits (const word_t *domains, const word_t *used_domains,
98 unsigned level, int y_state, const wfa_t *wfa, const void *model);
99 static word_t *
100 rle_generate (unsigned level, int y_state, const wfa_t *wfa,
101 const void *model);
102 static void *
103 rle_model_duplicate (const void *src);
104 static void
105 rle_model_free (void *model);
106 static void *
107 rle_model_alloc (unsigned max_domains);
108 static domain_pool_t *
109 alloc_rle_domain_pool (unsigned max_domains, unsigned max_edges,
110 const wfa_t *wfa);
111
112 /*****************************************************************************
113 const domain pool
114 *****************************************************************************/
115
116 static real_t
117 const_bits (const word_t *domains, const word_t *used_domains,
118 unsigned level, int y_state, const wfa_t *wfa, const void *model);
119 static word_t *
120 const_generate (unsigned level, int y_state, const wfa_t *wfa,
121 const void *model);
122 static domain_pool_t *
123 alloc_const_domain_pool (unsigned max_domains, unsigned max_edges,
124 const wfa_t *wfa);
125
126 /*****************************************************************************
127 basis domain pool
128 *****************************************************************************/
129
130 static domain_pool_t *
131 alloc_basis_domain_pool (unsigned max_domains, unsigned max_edges,
132 const wfa_t *wfa);
133
134 /*****************************************************************************
135 uniform distribution pool
136 *****************************************************************************/
137
138 static real_t
139 uniform_bits (const word_t *domains, const word_t *used_domains,
140 unsigned level, int y_state, const wfa_t *wfa,
141 const void *model);
142 static word_t *
143 uniform_generate (unsigned level, int y_state, const wfa_t *wfa,
144 const void *model);
145 static domain_pool_t *
146 alloc_uniform_domain_pool (unsigned max_domains, unsigned max_edges,
147 const wfa_t *wfa);
148
149 /*****************************************************************************
150 default functions
151 *****************************************************************************/
152
153 static void
154 init_matrix_probabilities (void);
155 static void
156 default_chroma (unsigned max_domains, const wfa_t *wfa, void *model);
157 static bool_t
158 default_append (unsigned new_state, unsigned level,
159 const wfa_t *wfa, void *model);
160 static void
161 default_update (const word_t *domains, const word_t *used_domains,
162 unsigned level, int y_state, const wfa_t *wfa, void *model);
163 static void
164 default_free (domain_pool_t *pool);
165 static void
166 default_model_free (void *model);
167 static void *
168 default_model_alloc (unsigned max_domains);
169 static void *
170 default_model_duplicate (const void *src);
171 static domain_pool_t *
172 default_alloc (void);
173
174 /*****************************************************************************
175
176 public code
177
178 *****************************************************************************/
179
180 typedef struct dp_array
181 {
182 const char *identifier;
183 domain_pool_t *(*function) (unsigned max_domains, unsigned max_edges,
184 const wfa_t *wfa);
185 } dp_array_t;
186
187 dp_array_t const domain_pools[] = {{"adaptive", alloc_qac_domain_pool},
188 {"constant", alloc_const_domain_pool},
189 {"basis", alloc_basis_domain_pool},
190 {"uniform", alloc_uniform_domain_pool},
191 {"rle", alloc_rle_domain_pool},
192 {"rle-no-chroma", alloc_rle_no_chroma_domain_pool},
193 {NULL, NULL}};
194
195 domain_pool_t *
196 alloc_domain_pool (const char *domain_pool_name, unsigned max_domains,
197 unsigned max_edges, const wfa_t *wfa)
198 /*
199 * Allocate a new domain pool identified by the string
200 * 'domain_pool_name'. Maximum number of domain images (each one
201 * represented by one state of the given 'wfa') is specified by
202 * 'max_domains'.
203 *
204 * Return value:
205 * pointer to the allocated domain pool
206 *
207 * Note:
208 * refer to 'domain-pool.h' for a short description of the member functions.
209 */
210 {
211 unsigned n;
212
213 if (!max_domains)
214 {
215 warning ("Can't generate empty domain pool. "
216 "Using at least DC component.");
217 max_domains = 1;
218 }
219
220 for (n = 0; domain_pools [n].identifier; n++) /* step through all id's */
221 if (strcaseeq (domain_pools [n].identifier, domain_pool_name))
222 return domain_pools [n].function (max_domains, max_edges, wfa);
223
224 warning ("Can't initialize domain pool '%s'. Using default value '%s'.",
225 domain_pool_name, domain_pools [0].identifier);
226
227 return domain_pools [0].function (max_domains, max_edges, wfa);
228 }
229
230 /*****************************************************************************
231
232 private code
233
234 *****************************************************************************/
235
236 /*****************************************************************************
237 adaptive domain pool
238 *****************************************************************************/
239
240 typedef struct qac_model
241 {
242 word_t *index; /* probability of domains */
243 word_t *states; /* mapping states -> domains */
244 u_word_t y_index; /* pointer to prob of Y domain */
245 u_word_t n; /* number of domains in the pool */
246 u_word_t max_domains; /* max. number of domains */
247 } qac_model_t;
248
249 static domain_pool_t *
250 alloc_qac_domain_pool (unsigned max_domains, unsigned max_edges,
251 const wfa_t *wfa)
252 /*
253 * Domain pool with state images {0, ..., 'max_domains').
254 * Underlying probability model: quasi arithmetic coding of columns.
255 */
256 {
257 domain_pool_t *pool;
258 unsigned state;
259
260 pool = default_alloc ();
261 pool->model = qac_model_alloc (max_domains);
262 pool->generate = qac_generate;
263 pool->bits = qac_bits;
264 pool->update = qac_update;
265 pool->append = qac_append;
266 pool->chroma = qac_chroma;
267 pool->model_free = qac_model_free;
268 pool->model_duplicate = qac_model_duplicate;
269
270 for (state = 0; state < wfa->basis_states; state++)
271 if (usedomain (state, wfa))
272 qac_append (state, -1, wfa, pool->model);
273
274 return pool;
275 }
276
277 static void *
278 qac_model_alloc (unsigned max_domains)
279 {
280 qac_model_t *model;
281
282 init_matrix_probabilities ();
283
284 model = Calloc (1, sizeof (qac_model_t));
285 model->index = Calloc (max_domains, sizeof (word_t));
286 model->states = Calloc (max_domains, sizeof (word_t));
287 model->y_index = 0;
288 model->n = 0;
289 model->max_domains = max_domains;
290
291 return model;
292 }
293
294 static void
295 qac_model_free (void *model)
296 {
297 Free (((qac_model_t *) model)->index);
298 Free (((qac_model_t *) model)->states);
299 Free (model);
300 }
301
302 static void *
303 qac_model_duplicate (const void *src)
304 {
305 qac_model_t *qdst;
306 const qac_model_t *qsrc = (qac_model_t *) src;
307
308 qdst = qac_model_alloc (qsrc->max_domains);
309 qdst->y_index = qsrc->y_index;
310 qdst->n = qsrc->n;
311
312 memcpy (qdst->index, qsrc->index, qsrc->n * sizeof (word_t));
313 memcpy (qdst->states, qsrc->states, qsrc->n * sizeof (word_t));
314
315 return qdst;
316 }
317
318 static word_t *
319 qac_generate (unsigned level, int y_state, const wfa_t *wfa, const void *model)
320 {
321 word_t *domains;
322 unsigned n;
323 qac_model_t *qac_model = (qac_model_t *) model;
324 bool_t y_state_is_domain = NO;
325
326 if (y_state >= 0 && !usedomain (y_state, wfa)) /* don't use y-state */
327 y_state = -1;
328
329 domains = Calloc (qac_model->n + 2, sizeof (word_t));
330
331 memcpy (domains, qac_model->states, qac_model->n * sizeof (word_t));
332
333 for (n = 0; n < qac_model->n; n++)
334 if (domains [n] == y_state) /* match */
335 y_state_is_domain = YES;
336
337 if (y_state_is_domain)
338 domains [qac_model->n] = -1; /* end marker */
339 else
340 {
341 domains [qac_model->n] = y_state; /* additional y-state */
342 domains [qac_model->n + 1] = -1; /* end marker */
343 }
344
345 return domains;
346 }
347
348 static real_t
349 qac_bits (const word_t *domains, const word_t *used_domains,
350 unsigned level, int y_state, const wfa_t *wfa, const void *model)
351 {
352 int domain; /* counter */
353 real_t bits = 0; /* bit rate R */
354 qac_model_t *qac_model = (qac_model_t *) model; /* probability model */
355
356 if (y_state >= 0 && !usedomain (y_state, wfa)) /* don't use y-state */
357 y_state = -1;
358
359 for (domain = 0; domain < qac_model->n; domain++)
360 if (qac_model->states [domain] != y_state)
361 bits += matrix_0 [qac_model->index [domain]];
362 if (y_state >= 0)
363 bits += matrix_0 [qac_model->y_index];
364
365 if (used_domains != NULL)
366 {
367 unsigned edge;
368
369 for (edge = 0; isedge (domain = used_domains [edge]); edge++)
370 if (domains [domain] == y_state)
371 {
372 bits -= matrix_0 [qac_model->y_index];
373 bits += matrix_1 [qac_model->y_index];
374 }
375 else
376 {
377 bits -= matrix_0 [qac_model->index [domain]];
378 bits += matrix_1 [qac_model->index [domain]];
379 }
380 }
381
382 return bits;
383 }
384
385 static void
386 qac_update (const word_t *domains, const word_t *used_domains,
387 unsigned level, int y_state, const wfa_t *wfa, void *model)
388 {
389 int domain;
390 unsigned edge;
391 bool_t used_y_state = NO;
392 qac_model_t *qac_model = (qac_model_t *) model;
393 bool_t y_state_is_domain = NO;
394
395 if (y_state >= 0 && !usedomain (y_state, wfa)) /* don't use y-state */
396 y_state = -1;
397
398 for (domain = 0; domain < qac_model->n; domain++)
399 {
400 qac_model->index [domain]++; /* mark domains unused. */
401 if (qac_model->states [domain] == y_state) /* match */
402 y_state_is_domain = YES;
403 }
404
405 for (edge = 0; isedge (domain = used_domains [edge]); edge++)
406 if (domains [domain] == y_state) /* chroma coding */
407 {
408 if (y_state_is_domain)
409 qac_model->index [domain]--; /* undo */
410 qac_model->y_index >>= 1;
411 used_y_state = YES;
412 }
413 else /* luminance coding */
414 {
415 qac_model->index [used_domains [edge]]--; /* undo */
416 qac_model->index [used_domains [edge]] >>= 1;
417 }
418
419 if (y_state >= 0 && !used_y_state)
420 qac_model->y_index++; /* update y-state model */
421
422 for (domain = 0; domain < qac_model->n; domain++)
423 if (qac_model->index [domain] > 1020) /* check for overflow */
424 qac_model->index [domain] = 1020;
425 if (qac_model->y_index > 1020) /* check for overflow */
426 qac_model->y_index = 1020;
427 }
428
429 static bool_t
430 qac_append (unsigned new_state, unsigned level, const wfa_t *wfa, void *model)
431 {
432 qac_model_t *qac_model = (qac_model_t *) model; /* probability model */
433
434 if (qac_model->n >= qac_model->max_domains)
435 return NO; /* don't use state in domain pool */
436 else
437 {
438 qac_model->index [qac_model->n]
439 = qac_model->n > 0 ? qac_model->index [qac_model->n - 1] : 0;
440 qac_model->states [qac_model->n] = new_state;
441 qac_model->n++;
442
443 return YES; /* state will be used in domain pool */
444 }
445 }
446
447 static void
448 qac_chroma (unsigned max_domains, const wfa_t *wfa, void *model)
449 {
450 qac_model_t *qac_model = (qac_model_t *) model; /* probability model */
451
452 if (max_domains < qac_model->n) /* choose most probable domains */
453 {
454 word_t *domains;
455 unsigned n, new, old;
456 word_t *states = Calloc (max_domains, sizeof (word_t));
457 word_t *index = Calloc (max_domains, sizeof (word_t));
458
459 domains = compute_hits (wfa->basis_states, wfa->states - 1,
460 max_domains, wfa);
461 for (n = 0; n < max_domains && domains [n] >= 0; n++)
462 states [n] = domains [n];
463 max_domains = MIN(max_domains, n);
464 Free (domains);
465
466 for (old = 0, new = 0; new < max_domains && old < qac_model->n; old++)
467 if (qac_model->states [old] == states [new])
468 index [new++] = qac_model->index [old];
469
470 Free (qac_model->states);
471 Free (qac_model->index);
472 qac_model->states = states;
473 qac_model->index = index;
474 qac_model->n = max_domains;
475 qac_model->max_domains = max_domains;
476 }
477 qac_model->y_index = 0;
478 qac_model->max_domains = qac_model->n;
479 }
480
481 /*****************************************************************************
482 const domain pool
483 *****************************************************************************/
484
485 static domain_pool_t *
486 alloc_const_domain_pool (unsigned max_domains, unsigned max_edges,
487 const wfa_t *wfa)
488 /*
489 * Domain pool with state image 0 (constant function f(x, y) = 1).
490 * No probability model is used.
491 */
492 {
493 domain_pool_t *pool;
494
495 pool = default_alloc ();
496 pool->generate = const_generate;
497 pool->bits = const_bits;
498
499 return pool;
500 }
501
502 static word_t *
503 const_generate (unsigned level, int y_state, const wfa_t *wfa,
504 const void *model)
505 {
506 word_t *domains = Calloc (2, sizeof (word_t));
507
508 domains [0] = 0;
509 domains [1] = -1;
510
511 return domains;
512 }
513
514 static real_t
515 const_bits (const word_t *domains, const word_t *used_domains,
516 unsigned level, int y_state, const wfa_t *wfa, const void *model)
517 {
518 return 0; /* 0 bits,
519 either we have a lc or not */
520 }
521
522 /*****************************************************************************
523 basis domain pool
524 *****************************************************************************/
525
526 static domain_pool_t *
527 alloc_basis_domain_pool (unsigned max_domains, unsigned max_edges,
528 const wfa_t *wfa)
529 /*
530 * Domain pool with state images {0, ..., 'basis_states' - 1).
531 * Underlying probability model: quasi arithmetic coding of columns.
532 * I.e. domain pool = qac_domainpool ('max_domains' == wfa->basis_states)
533 */
534 {
535 return alloc_qac_domain_pool (wfa->basis_states, max_edges, wfa);
536 }
537
538 /*****************************************************************************
539 uniform-distribution pool
540 *****************************************************************************/
541
542 static domain_pool_t *
543 alloc_uniform_domain_pool (unsigned max_domains, unsigned max_edges,
544 const wfa_t *wfa)
545 /*
546 * Domain pool with state images {0, ..., 'max_domains').
547 * Underlying probability model: uniform distribution.
548 */
549 {
550 domain_pool_t *pool;
551
552 pool = default_alloc ();
553 pool->generate = uniform_generate;
554 pool->bits = uniform_bits;
555
556 return pool;
557 }
558
559 static word_t *
560 uniform_generate (unsigned level, int y_state, const wfa_t *wfa,
561 const void *model)
562 {
563 unsigned state, n;
564 word_t *domains = Calloc (wfa->states + 1, sizeof (word_t));
565
566 for (state = 0, n = 0; state < wfa->states; state++)
567 if (usedomain (state, wfa))
568 domains [n++] = state;
569 domains [n] = -1;
570
571 return domains;
572 }
573
574 static real_t
575 uniform_bits (const word_t *domains, const word_t *used_domains,
576 unsigned level, int y_state, const wfa_t *wfa, const void *model)
577 {
578 unsigned state, n;
579 real_t bits = 0;
580
581 for (state = 0, n = 0; state < wfa->states; state++)
582 if (usedomain (state, wfa))
583 n++;
584
585 bits = - n * log2 ((n - 1) / (real_t) n);
586
587 if (used_domains != NULL)
588 {
589 int edge;
590
591 for (edge = 0; isedge (used_domains [edge]); edge++)
592 bits -= log2 (1.0 / n);
593 }
594
595 return bits;
596 }
597
598 /*****************************************************************************
599 run length encoding pool
600 *****************************************************************************/
601
602 typedef struct rle_model
603 {
604 word_t count [MAXEDGES + 1];
605 u_word_t total;
606 u_word_t n;
607 u_word_t max_domains;
608 u_word_t y_index; /* pointer to prob of Y domain */
609 word_t *states; /* mapping states -> domains */
610 qac_model_t *domain_0;
611 } rle_model_t;
612
613 static domain_pool_t *
614 alloc_rle_domain_pool (unsigned max_domains, unsigned max_edges,
615 const wfa_t *wfa)
616 /*
617 * Domain pool with state images {0, ..., 'max_domains').
618 * Underlying probability model: rle
619 */
620 {
621 domain_pool_t *pool;
622 unsigned state;
623
624 pool = default_alloc ();
625 pool->model = rle_model_alloc (max_domains);
626 pool->model_free = rle_model_free;
627 pool->model_duplicate = rle_model_duplicate;
628 pool->generate = rle_generate;
629 pool->update = rle_update;
630 pool->bits = rle_bits;
631 pool->append = rle_append;
632 pool->chroma = rle_chroma;
633
634 for (state = 0; state < wfa->basis_states; state++)
635 if (usedomain (state, wfa))
636 rle_append (state, -1, wfa, pool->model);
637
638 return pool;
639 }
640
641 static void *
642 rle_model_alloc (unsigned max_domains)
643 {
644 unsigned m;
645 rle_model_t *model = Calloc (1, sizeof (rle_model_t));
646
647 for (m = model->total = 0; m < MAXEDGES + 1; m++, model->total++)
648 model->count [m] = 1;
649
650 model->domain_0 = qac_model_alloc (1);
651 model->states = Calloc (max_domains, sizeof (word_t));
652 model->n = 0;
653 model->y_index = 0;
654 model->max_domains = max_domains;
655
656 return model;
657 }
658
659 static void
660 rle_model_free (void *model)
661 {
662 qac_model_free (((rle_model_t *) model)->domain_0);
663 Free (((rle_model_t *) model)->states);
664 Free (model);
665 }
666
667 static void *
668 rle_model_duplicate (const void *src)
669 {
670 const rle_model_t *rle_src = (rle_model_t *) src;
671 rle_model_t *model = Calloc (1, sizeof (rle_model_t));
672
673 model->domain_0 = qac_model_duplicate (rle_src->domain_0);
674 model->n = rle_src->n;
675 model->max_domains = rle_src->max_domains;
676 model->states = Calloc (model->max_domains, sizeof (word_t));
677 model->total = rle_src->total;
678 model->y_index = rle_src->y_index;
679
680 memcpy (model->states, rle_src->states,
681 model->max_domains * sizeof (word_t));
682 memcpy (model->count, rle_src->count,
683 (MAXEDGES + 1) * sizeof (word_t));
684
685 return model;
686 }
687
688 static word_t *
689 rle_generate (unsigned level, int y_state, const wfa_t *wfa, const void *model)
690 {
691 word_t *domains;
692 unsigned n;
693 rle_model_t *rle_model = (rle_model_t *) model;
694 bool_t y_state_is_domain = NO;
695
696 if (y_state >= 0 && !usedomain (y_state, wfa)) /* don't use y-state */
697 y_state = -1;
698
699 domains = Calloc (rle_model->n + 2, sizeof (word_t));
700
701 memcpy (domains, rle_model->states, rle_model->n * sizeof (word_t));
702
703 for (n = 0; n < rle_model->n; n++)
704 if (domains [n] == y_state) /* match */
705 y_state_is_domain = YES;
706
707 if (y_state_is_domain)
708 domains [rle_model->n] = -1; /* end marker */
709 else
710 {
711 domains [rle_model->n] = y_state; /* additional y-state */
712 domains [rle_model->n + 1] = -1; /* end marker */
713 }
714
715 return domains;
716 }
717
718 static real_t
719 rle_bits (const word_t *domains, const word_t *used_domains,
720 unsigned level, int y_state, const wfa_t *wfa, const void *model)
721 {
722 unsigned edge;
723 unsigned n = 0;
724 real_t bits = 0;
725 word_t sorted [MAXEDGES + 1];
726 rle_model_t *rle_model = (rle_model_t *) model;
727 unsigned last;
728 int into;
729
730 if (y_state >= 0 && !usedomain (y_state, wfa)) /* don't use y-state */
731 y_state = -1;
732
733 if (used_domains)
734 {
735 word_t domain;
736
737 if (y_state >= 0)
738 bits += matrix_0 [rle_model->y_index];
739
740 for (edge = n = 0; isedge (domain = used_domains [edge]); edge++)
741 if (domains [domain] != y_state)
742 sorted [n++] = used_domains [edge];
743 else
744 {
745 bits -= matrix_0 [rle_model->y_index];
746 bits += matrix_1 [rle_model->y_index];
747 }
748
749 if (n > 1)
750 qsort (sorted, n, sizeof (word_t), sort_asc_word);
751 }
752
753 bits = - log2 (rle_model->count [n] / (real_t) rle_model->total);
754 if (used_domains && n && sorted [0] == 0)
755 {
756 word_t array0 [2] = {0, NO_EDGE};
757 bits += qac_bits (array0, array0, level, y_state, wfa, rle_model->domain_0);
758 }
759 else
760 {
761 word_t array0 [2] = {NO_EDGE};
762 bits += qac_bits (array0, array0, level, y_state, wfa, rle_model->domain_0);
763 }
764
765 last = 1;
766 for (edge = 0; edge < n; edge++)
767 if ((into = sorted [edge]) && rle_model->n - 1 - last)
768 {
769 bits += bits_bin_code (into - last, rle_model->n - 1 - last);
770 last = into + 1;
771 }
772
773 return bits;
774 }
775
776 static void
777 rle_update (const word_t *domains, const word_t *used_domains,
778 unsigned level, int y_state, const wfa_t *wfa, void *model)
779 {
780 rle_model_t *rle_model = (rle_model_t *) model;
781 bool_t state_0 = NO, state_y = NO;
782 word_t array0 [2] = {0, NO_EDGE};
783 unsigned edge = 0;
784
785 if (y_state >= 0 && !usedomain (y_state, wfa)) /* don't use y-state */
786 y_state = -1;
787
788 if (used_domains)
789 {
790 word_t domain;
791
792 for (edge = 0; isedge (domain = used_domains [edge]); edge++)
793 if (domains [domain] == 0)
794 state_0 = YES;
795 else if (domains [domain] == y_state)
796 state_y = YES;
797 }
798
799 rle_model->count [edge]++;
800 rle_model->total++;
801
802 qac_update (array0, array0 + (state_0 ? 0 : 1), level, y_state, wfa,
803 rle_model->domain_0);
804
805 if (state_y)
806 rle_model->y_index >>= 1;
807 else
808 rle_model->y_index++;
809 if (rle_model->y_index > 1020) /* check for overflow */
810 rle_model->y_index = 1020;
811 }
812
813 static bool_t
814 rle_append (unsigned new_state, unsigned level, const wfa_t *wfa, void *model)
815 {
816 rle_model_t *rle_model = (rle_model_t *) model; /* probability model */
817
818 if (rle_model->n >= rle_model->max_domains)
819 return NO; /* don't use state in domain pool */
820 else
821 {
822 rle_model->states [rle_model->n] = new_state;
823 rle_model->n++;
824
825 if (new_state == 0)
826 {
827 assert (rle_model->n == 1);
828 qac_append (0, -1, wfa, rle_model->domain_0);
829 }
830
831 return YES; /* state will be used in domain pool */
832 }
833 }
834
835 static void
836 rle_chroma (unsigned max_domains, const wfa_t *wfa, void *model)
837 {
838 rle_model_t *rle_model = (rle_model_t *) model; /* probability model */
839
840 if (max_domains < rle_model->n) /* choose most probable domains */
841 {
842 unsigned n;
843 word_t *states = Calloc (max_domains, sizeof (word_t));
844 word_t *domains = compute_hits (wfa->basis_states, wfa->states - 1,
845 max_domains, wfa);
846
847 for (n = 0; n < max_domains && domains [n] >= 0; n++)
848 states [n] = domains [n];
849
850 assert (states [0] == 0);
851 max_domains = MIN(max_domains, n);
852 Free (domains);
853
854 Free (rle_model->states);
855 rle_model->states = states;
856 rle_model->n = max_domains;
857 }
858 rle_model->y_index = 0;
859 rle_model->max_domains = rle_model->n;
860 }
861
862 /*****************************************************************************
863 run length encoding pool no special chroma pool
864 *****************************************************************************/
865
866 static domain_pool_t *
867 alloc_rle_no_chroma_domain_pool (unsigned max_domains, unsigned max_edges,
868 const wfa_t *wfa)
869 /*
870 * Domain pool with state images {0, ..., 'max_domains').
871 * Underlying probability model: rle
872 * Domain pool is not changed for chroma bands
873 */
874 {
875 domain_pool_t *pool = alloc_rle_domain_pool (max_domains, max_edges, wfa);
876
877 pool->chroma = default_chroma;
878
879 return pool;
880 }
881
882 /*****************************************************************************
883 default functions (see domain-pool.h)
884 *****************************************************************************/
885
886 static domain_pool_t *
887 default_alloc (void)
888 {
889 domain_pool_t *pool;
890
891 pool = Calloc (1, sizeof (domain_pool_t));
892 pool->model = default_model_alloc(0);
893 pool->generate = NULL;
894 pool->bits = NULL;
895 pool->update = default_update;
896 pool->append = default_append;
897 pool->chroma = default_chroma;
898 pool->free = default_free;
899 pool->model_free = default_model_free;
900 pool->model_duplicate = default_model_duplicate;
901
902 return pool;
903 }
904
905 static void *
906 default_model_duplicate (const void *src)
907 {
908 return NULL;
909 }
910
911 static void *
912 default_model_alloc (unsigned max_domains)
913 {
914 return NULL;
915 }
916
917 static void
918 default_model_free (void *model)
919 {
920 if (model)
921 Free (model);
922 }
923
924 static void
925 default_free (domain_pool_t *pool)
926 {
927 pool->model_free (pool->model);
928 Free (pool);
929 }
930
931 static void
932 default_update (const word_t *domains, const word_t *used_domains,
933 unsigned level, int y_state, const wfa_t *wfa, void *model)
934 {
935 return; /* nothing to do */
936 }
937
938 static bool_t
939 default_append (unsigned new_state, unsigned level,
940 const wfa_t *wfa, void *model)
941 {
942 return YES; /* use every state in lin comb */
943 }
944
945 static void
946 default_chroma (unsigned max_domains, const wfa_t *wfa, void *model)
947 {
948 return; /* don't alter domain pool */
949 }
950
951 static void
952 init_matrix_probabilities (void)
953 /*
954 * Compute the information contents of matrix element '0' and '1' for
955 * each possible probability index 0, ... , 1023. These values are
956 * obtained from the probability model in the quasi arithmetic
957 * coding module.
958 *
959 * No return value.
960 *
961 * Side effects:
962 * local arrays matrix_0 and matrix_1 are initialized if not already done.
963 */
964 {
965 if (matrix_0 == NULL || matrix_1 == NULL)
966 {
967 unsigned index;
968 unsigned n, exp;
969
970 matrix_0 = Calloc (1 << (MAX_PROB + 1), sizeof (real_t));
971 matrix_1 = Calloc (1 << (MAX_PROB + 1), sizeof (real_t));
972
973 for (index = 0, n = MIN_PROB; n <= MAX_PROB; n++)
974 for (exp = 0; exp < (unsigned) 1 << n; exp++, index++)
975 {
976 matrix_1 [index] = -log2 (1 / (real_t) (1 << n));
977 matrix_0 [index] = -log2 (1 - 1 / (real_t) (1 << n));
978 }
979 }
980 }