about summary refs log tree commit diff
path: root/converter/other/fiasco/codec/domain-pool.c
blob: 1c14a30c7be59bf028bb52af6e581619adaae53c (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
/*
 *  domain-pool.c:  Domain pool management (probability model)
 *
 *  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 <math.h>
#include <stdlib.h>
#include <string.h>

#include "pm_c_util.h"

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

#include "misc.h"
#include "cwfa.h"
#include "wfalib.h"
#include "domain-pool.h"

/*
 *  Domain pool model interface:
 *  Implementing the domain pool model interface requires the
 *  following steps: 
 *  - Add a constructor that initializes the domain_pool_t structure
 *  - Allocate new model with default_alloc() 
 *  - Fill the dp_array_t domain_pools array with constructor and name
 *  - Write code for methods bits() and generate()
 *  - Either use default functions for remaining methods or override them
 *  The new model is automatically registered at the command line.
 */

/*****************************************************************************
                                          
                  local variables
                  
*****************************************************************************/

static real_t *matrix_0 = NULL;
static real_t *matrix_1 = NULL;

/*****************************************************************************
                        non-adaptive domain pool
*****************************************************************************/

static void
qac_chroma (unsigned max_domains, const wfa_t *wfa, void *model);
static bool_t
qac_append (unsigned new_state, unsigned level, const wfa_t *wfa, void *model);
static void
qac_update (const word_t *domains, const word_t *used_domains,
            unsigned level, int y_state, const wfa_t *wfa, void *model);
static real_t
qac_bits (const word_t *domains, const word_t *used_domains,
          unsigned level, int y_state, const wfa_t *wfa, const void *model);
static word_t *
qac_generate (unsigned level, int y_state, const wfa_t *wfa,
              const void *model);
static void *
qac_model_duplicate (const void *src);
static void
qac_model_free (void *model);
static void *
qac_model_alloc (unsigned max_domains);
static domain_pool_t *
alloc_qac_domain_pool (unsigned max_domains, unsigned max_edges,
                       const wfa_t *wfa);

/*****************************************************************************
              run length encoding pool
*****************************************************************************/

static domain_pool_t *
alloc_rle_no_chroma_domain_pool (unsigned max_domains, unsigned max_edges,
                                 const wfa_t *wfa);
static void
rle_chroma (unsigned max_domains, const wfa_t *wfa, void *model);
static bool_t
rle_append (unsigned new_state, unsigned level, const wfa_t *wfa, void *model);
static void
rle_update (const word_t *domains, const word_t *used_domains,
            unsigned level, int y_state, const wfa_t *wfa, void *model);
static real_t
rle_bits (const word_t *domains, const word_t *used_domains,
          unsigned level, int y_state, const wfa_t *wfa, const void *model);
static word_t *
rle_generate (unsigned level, int y_state, const wfa_t *wfa,
              const void *model);
static void *
rle_model_duplicate (const void *src);
static void
rle_model_free (void *model);
static void *
rle_model_alloc (unsigned max_domains);
static domain_pool_t *
alloc_rle_domain_pool (unsigned max_domains, unsigned max_edges,
                       const wfa_t *wfa);

/*****************************************************************************
              const domain pool
*****************************************************************************/

static real_t
const_bits (const word_t *domains, const word_t *used_domains,
            unsigned level, int y_state, const wfa_t *wfa, const void *model);
static word_t *
const_generate (unsigned level, int y_state, const wfa_t *wfa,
                const void *model);
static domain_pool_t *
alloc_const_domain_pool (unsigned max_domains, unsigned max_edges,
                         const wfa_t *wfa);

/*****************************************************************************
              basis domain pool
*****************************************************************************/

static domain_pool_t *
alloc_basis_domain_pool (unsigned max_domains, unsigned max_edges,
                         const wfa_t *wfa);

/*****************************************************************************
              uniform distribution pool
*****************************************************************************/

static real_t
uniform_bits (const word_t *domains, const word_t *used_domains,
              unsigned level, int y_state, const wfa_t *wfa,
              const void *model);
static word_t *
uniform_generate (unsigned level, int y_state, const wfa_t *wfa,
                  const void *model);
static domain_pool_t *
alloc_uniform_domain_pool (unsigned max_domains, unsigned max_edges,
                           const wfa_t *wfa);

/*****************************************************************************
              default functions
*****************************************************************************/

static void
init_matrix_probabilities (void);
static void
default_chroma (unsigned max_domains, const wfa_t *wfa, void *model);
static bool_t
default_append (unsigned new_state, unsigned level,
                const wfa_t *wfa, void *model);
static void
default_update (const word_t *domains, const word_t *used_domains,
                unsigned level, int y_state, const wfa_t *wfa, void *model);
static void
default_free (domain_pool_t *pool);
static void
default_model_free (void *model);
static void *
default_model_alloc (unsigned max_domains);
static void *
default_model_duplicate (const void *src);
static domain_pool_t *
default_alloc (void);

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

                public code
  
*****************************************************************************/

typedef struct dp_array
{
    const char       *identifier;
    domain_pool_t *(*function) (unsigned max_domains, unsigned max_edges,
                                const wfa_t *wfa);
} dp_array_t;

dp_array_t const domain_pools[] = {{"adaptive", alloc_qac_domain_pool},
                                   {"constant",   alloc_const_domain_pool}, 
                                   {"basis",      alloc_basis_domain_pool},
                                   {"uniform",    alloc_uniform_domain_pool},
                                   {"rle",        alloc_rle_domain_pool},
                                   {"rle-no-chroma",  alloc_rle_no_chroma_domain_pool},
                                   {NULL,     NULL}};

domain_pool_t *
alloc_domain_pool (const char *domain_pool_name, unsigned max_domains,
                   unsigned max_edges, const wfa_t *wfa)
/*
 *  Allocate a new domain pool identified by the string
 *  'domain_pool_name'.  Maximum number of domain images (each one
 *  represented by one state of the given 'wfa') is specified by
 *  'max_domains'. 
 * 
 *  Return value:
 *  pointer to the allocated domain pool
 *
 *  Note:
 *      refer to 'domain-pool.h' for a short description of the member functions.
 */
{
    unsigned n;
   
    if (!max_domains)
    {
        warning ("Can't generate empty domain pool. "
                 "Using at least DC component.");
        max_domains = 1;
    }
   
    for (n = 0; domain_pools [n].identifier; n++) /* step through all id's */
        if (strcaseeq (domain_pools [n].identifier, domain_pool_name)) 
            return domain_pools [n].function (max_domains, max_edges, wfa);

    warning ("Can't initialize domain pool '%s'. Using default value '%s'.",
             domain_pool_name, domain_pools [0].identifier);

    return domain_pools [0].function (max_domains, max_edges, wfa);
}

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

                private code
  
*****************************************************************************/

/*****************************************************************************
              adaptive domain pool
*****************************************************************************/

typedef struct qac_model
{
    word_t   *index;         /* probability of domains */
    word_t   *states;            /* mapping states -> domains */
    u_word_t  y_index;           /* pointer to prob of Y domain */
    u_word_t  n;             /* number of domains in the pool */
    u_word_t  max_domains;       /* max. number of domains */
} qac_model_t;

static domain_pool_t *
alloc_qac_domain_pool (unsigned max_domains, unsigned max_edges,
                       const wfa_t *wfa)
/*
 *  Domain pool with state images {0, ..., 'max_domains').
 *  Underlying probability model: quasi arithmetic coding of columns.
 */
{
    domain_pool_t *pool;
    unsigned   state;
   
    pool                  = default_alloc ();
    pool->model           = qac_model_alloc (max_domains);
    pool->generate        = qac_generate;
    pool->bits            = qac_bits;
    pool->update          = qac_update;
    pool->append          = qac_append;
    pool->chroma      = qac_chroma;
    pool->model_free      = qac_model_free;
    pool->model_duplicate = qac_model_duplicate;
   
    for (state = 0; state < wfa->basis_states; state++)
        if (usedomain (state, wfa))
            qac_append (state, -1, wfa, pool->model);

    return pool;
}

static void *
qac_model_alloc (unsigned max_domains)
{
    qac_model_t *model;

    init_matrix_probabilities ();

    model          = Calloc (1, sizeof (qac_model_t));
    model->index       = Calloc (max_domains, sizeof (word_t));
    model->states      = Calloc (max_domains, sizeof (word_t));
    model->y_index     = 0;
    model->n       = 0;
    model->max_domains = max_domains;

    return model;
}

static void
qac_model_free (void *model)
{
    Free (((qac_model_t *) model)->index);
    Free (((qac_model_t *) model)->states);
    Free (model);
}

static void *
qac_model_duplicate (const void *src)
{
    qac_model_t       *qdst;
    const qac_model_t *qsrc = (qac_model_t *) src;

    qdst      = qac_model_alloc (qsrc->max_domains);
    qdst->y_index = qsrc->y_index;
    qdst->n       = qsrc->n;
   
    memcpy (qdst->index, qsrc->index, qsrc->n * sizeof (word_t));
    memcpy (qdst->states, qsrc->states, qsrc->n * sizeof (word_t));

    return qdst;
}

static word_t *
qac_generate (unsigned level, int y_state, const wfa_t *wfa, const void *model)
{
    word_t      *domains;
    unsigned n;
    qac_model_t *qac_model     = (qac_model_t *) model;
    bool_t   y_state_is_domain = NO;

    if (y_state >= 0 && !usedomain (y_state, wfa)) /* don't use y-state */
        y_state = -1;
   
    domains = Calloc (qac_model->n + 2, sizeof (word_t));

    memcpy (domains, qac_model->states, qac_model->n * sizeof (word_t));

    for (n = 0; n < qac_model->n; n++)
        if (domains [n] == y_state)   /* match */
            y_state_is_domain = YES;       

    if (y_state_is_domain)
        domains [qac_model->n] = -1;  /* end marker */
    else
    {
        domains [qac_model->n]     = y_state; /* additional y-state */
        domains [qac_model->n + 1] = -1;  /* end marker */
    }

    return domains;
}

static real_t
qac_bits (const word_t *domains, const word_t *used_domains,
          unsigned level, int y_state, const wfa_t *wfa, const void *model)
{
    int      domain;         /* counter */
    real_t   bits      = 0;      /* bit rate R */
    qac_model_t *qac_model = (qac_model_t *) model; /* probability model */

    if (y_state >= 0 && !usedomain (y_state, wfa)) /* don't use y-state */
        y_state = -1;

    for (domain = 0; domain < qac_model->n; domain++)
        if (qac_model->states [domain] != y_state)
            bits += matrix_0 [qac_model->index [domain]];
    if (y_state >= 0)
        bits += matrix_0 [qac_model->y_index];
   
    if (used_domains != NULL)
    {
        unsigned edge;
      
        for (edge = 0; isedge (domain = used_domains [edge]); edge++)
            if (domains [domain] == y_state)
            {
                bits -= matrix_0 [qac_model->y_index];
                bits += matrix_1 [qac_model->y_index];
            }
            else
            {
                bits -= matrix_0 [qac_model->index [domain]];
                bits += matrix_1 [qac_model->index [domain]];
            }
    } 
   
    return bits;
}

static void
qac_update (const word_t *domains, const word_t *used_domains,
            unsigned level, int y_state, const wfa_t *wfa, void *model)
{
    int      domain;
    unsigned edge;
    bool_t   used_y_state      = NO;
    qac_model_t *qac_model     = (qac_model_t *) model;
    bool_t   y_state_is_domain = NO;
   
    if (y_state >= 0 && !usedomain (y_state, wfa)) /* don't use y-state */
        y_state = -1;

    for (domain = 0; domain < qac_model->n; domain++)
    {
        qac_model->index [domain]++;  /* mark domains unused. */
        if (qac_model->states [domain] == y_state) /* match */
            y_state_is_domain = YES;       
    }
   
    for (edge = 0; isedge (domain = used_domains [edge]); edge++)
        if (domains [domain] == y_state) /* chroma coding */
        {
            if (y_state_is_domain)
                qac_model->index [domain]--; /* undo */
            qac_model->y_index >>= 1;
            used_y_state = YES;
        }
        else              /* luminance coding */
        {
            qac_model->index [used_domains [edge]]--; /* undo */
            qac_model->index [used_domains [edge]] >>= 1;      
        }

    if (y_state >= 0 && !used_y_state)
        qac_model->y_index++;     /* update y-state model */
   
    for (domain = 0; domain < qac_model->n; domain++)
        if (qac_model->index [domain] > 1020) /* check for overflow */
            qac_model->index [domain] = 1020; 
    if (qac_model->y_index > 1020)   /* check for overflow */
        qac_model->y_index = 1020; 
}

static bool_t
qac_append (unsigned new_state, unsigned level, const wfa_t *wfa, void *model)
{
    qac_model_t  *qac_model = (qac_model_t *) model; /* probability model */
   
    if (qac_model->n >= qac_model->max_domains)
        return NO;            /* don't use state in domain pool */
    else
    {
        qac_model->index [qac_model->n]
            = qac_model->n > 0 ? qac_model->index [qac_model->n - 1] : 0;
        qac_model->states [qac_model->n] = new_state;
        qac_model->n++;

        return YES;           /* state will be used in domain pool */
    }
}

static void
qac_chroma (unsigned max_domains, const wfa_t *wfa, void *model)
{
    qac_model_t *qac_model = (qac_model_t *) model; /* probability model */
   
    if (max_domains < qac_model->n)  /* choose most probable domains */
    {
        word_t   *domains;
        unsigned  n, new, old;
        word_t   *states = Calloc (max_domains, sizeof (word_t));
        word_t   *index  = Calloc (max_domains, sizeof (word_t));
   
        domains = compute_hits (wfa->basis_states, wfa->states - 1,
                                max_domains, wfa);
        for (n = 0; n < max_domains && domains [n] >= 0; n++)
            states [n] = domains [n];
        max_domains = MIN(max_domains, n);
        Free (domains);

        for (old = 0, new = 0; new < max_domains && old < qac_model->n; old++)
            if (qac_model->states [old] == states [new])
                index [new++] = qac_model->index [old];

        Free (qac_model->states);
        Free (qac_model->index);
        qac_model->states      = states;
        qac_model->index       = index;
        qac_model->n           = max_domains;
        qac_model->max_domains = max_domains;
    }
    qac_model->y_index     = 0;
    qac_model->max_domains = qac_model->n;
}

/*****************************************************************************
              const domain pool
*****************************************************************************/

static domain_pool_t *
alloc_const_domain_pool (unsigned max_domains, unsigned max_edges,
                         const wfa_t *wfa)
/*
 *  Domain pool with state image 0 (constant function f(x, y) = 1).
 *  No probability model is used.
 */
{
    domain_pool_t *pool;
   
    pool           = default_alloc ();   
    pool->generate = const_generate;
    pool->bits     = const_bits;
   
    return pool;
}

static word_t *
const_generate (unsigned level, int y_state, const wfa_t *wfa,
                const void *model)
{
    word_t *domains = Calloc (2, sizeof (word_t));
   
    domains [0] = 0;
    domains [1] = -1;
   
    return domains;
}

static real_t
const_bits (const word_t *domains, const word_t *used_domains,
            unsigned level, int y_state, const wfa_t *wfa, const void *model)
{
    return 0;                /* 0 bits,
                                either we have a lc or not */
}

/*****************************************************************************
                basis domain pool
*****************************************************************************/

static domain_pool_t *
alloc_basis_domain_pool (unsigned max_domains, unsigned max_edges,
                         const wfa_t *wfa)
/*
 *  Domain pool with state images {0, ..., 'basis_states' - 1).
 *  Underlying probability model: quasi arithmetic coding of columns.
 *  I.e. domain pool = qac_domainpool ('max_domains' == wfa->basis_states)
 */
{
    return alloc_qac_domain_pool (wfa->basis_states, max_edges, wfa);
}

/*****************************************************************************
              uniform-distribution pool
*****************************************************************************/

static domain_pool_t *
alloc_uniform_domain_pool (unsigned max_domains, unsigned max_edges,
                           const wfa_t *wfa)
/*
 *  Domain pool with state images {0, ..., 'max_domains').
 *  Underlying probability model: uniform distribution.
 */
{
    domain_pool_t *pool;
   
    pool           = default_alloc ();   
    pool->generate = uniform_generate;
    pool->bits     = uniform_bits;
   
    return pool;
}

static word_t *
uniform_generate (unsigned level, int y_state, const wfa_t *wfa,
                  const void *model)
{
    unsigned  state, n;
    word_t   *domains = Calloc (wfa->states + 1, sizeof (word_t));

    for (state = 0, n = 0; state < wfa->states; state++)
        if (usedomain (state, wfa))
            domains [n++] = state;
    domains [n] = -1;
   
    return domains;
}
 
static real_t
uniform_bits (const word_t *domains, const word_t *used_domains,
              unsigned level, int y_state, const wfa_t *wfa, const void *model)
{
    unsigned state, n;
    real_t   bits = 0;
   
    for (state = 0, n = 0; state < wfa->states; state++)
        if (usedomain (state, wfa))
            n++;

    bits = - n * log2 ((n - 1) / (real_t) n);

    if (used_domains != NULL)
    {
        int edge;
      
        for (edge = 0; isedge (used_domains [edge]); edge++)
            bits -= log2 (1.0 / n);
    }

    return bits;
}

/*****************************************************************************
              run length encoding pool
*****************************************************************************/

typedef struct rle_model
{
    word_t   count [MAXEDGES + 1];
    u_word_t total;
    u_word_t n;
    u_word_t max_domains;
    u_word_t y_index;        /* pointer to prob of Y domain */
    word_t      *states;         /* mapping states -> domains */
    qac_model_t *domain_0;
} rle_model_t;

static domain_pool_t *
alloc_rle_domain_pool (unsigned max_domains, unsigned max_edges,
                       const wfa_t *wfa)
/*
 *  Domain pool with state images {0, ..., 'max_domains').
 *  Underlying probability model: rle 
 */
{
    domain_pool_t *pool;
    unsigned   state;
   
    pool                  = default_alloc ();    
    pool->model           = rle_model_alloc (max_domains);
    pool->model_free      = rle_model_free;
    pool->model_duplicate = rle_model_duplicate;
    pool->generate        = rle_generate;
    pool->update          = rle_update;
    pool->bits            = rle_bits;
    pool->append          = rle_append;
    pool->chroma          = rle_chroma;

    for (state = 0; state < wfa->basis_states; state++)
        if (usedomain (state, wfa))
            rle_append (state, -1, wfa, pool->model);

    return pool;
}

static void *
rle_model_alloc (unsigned max_domains)
{
    unsigned m;
    rle_model_t *model = Calloc (1, sizeof (rle_model_t));
   
    for (m = model->total = 0; m < MAXEDGES + 1; m++, model->total++)
        model->count [m] = 1;

    model->domain_0    = qac_model_alloc (1);
    model->states      = Calloc (max_domains, sizeof (word_t));
    model->n       = 0;
    model->y_index     = 0;
    model->max_domains = max_domains;
   
    return model;
}

static void
rle_model_free (void *model)
{
    qac_model_free (((rle_model_t *) model)->domain_0);
    Free (((rle_model_t *) model)->states);
    Free (model);
}

static void *
rle_model_duplicate (const void *src)
{
    const rle_model_t *rle_src = (rle_model_t *) src;
    rle_model_t       *model   = Calloc (1, sizeof (rle_model_t));

    model->domain_0    = qac_model_duplicate (rle_src->domain_0);
    model->n       = rle_src->n;
    model->max_domains = rle_src->max_domains;
    model->states      = Calloc (model->max_domains, sizeof (word_t));
    model->total       = rle_src->total;
    model->y_index     = rle_src->y_index;
   
    memcpy (model->states, rle_src->states,
            model->max_domains * sizeof (word_t));
    memcpy (model->count, rle_src->count,
            (MAXEDGES + 1) * sizeof (word_t));
   
    return model;
}

static word_t *
rle_generate (unsigned level, int y_state, const wfa_t *wfa, const void *model)
{
    word_t      *domains;
    unsigned n;
    rle_model_t *rle_model     = (rle_model_t *) model;
    bool_t   y_state_is_domain = NO;
   
    if (y_state >= 0 && !usedomain (y_state, wfa)) /* don't use y-state */
        y_state = -1;
   
    domains = Calloc (rle_model->n + 2, sizeof (word_t));

    memcpy (domains, rle_model->states, rle_model->n * sizeof (word_t));

    for (n = 0; n < rle_model->n; n++)
        if (domains [n] == y_state)   /* match */
            y_state_is_domain = YES;       

    if (y_state_is_domain)
        domains [rle_model->n] = -1;  /* end marker */
    else
    {
        domains [rle_model->n]     = y_state; /* additional y-state */
        domains [rle_model->n + 1] = -1;  /* end marker */
    }

    return domains;
}

static real_t
rle_bits (const word_t *domains, const word_t *used_domains,
          unsigned level, int y_state, const wfa_t *wfa, const void *model)
{
    unsigned edge;
    unsigned n     = 0;
    real_t   bits      = 0;
    word_t   sorted [MAXEDGES + 1];
    rle_model_t *rle_model = (rle_model_t *) model;
    unsigned last;
    int      into;
   
    if (y_state >= 0 && !usedomain (y_state, wfa)) /* don't use y-state */
        y_state = -1;

    if (used_domains)
    {
        word_t domain;
      
        if (y_state >= 0)
            bits += matrix_0 [rle_model->y_index];
      
        for (edge = n = 0; isedge (domain = used_domains [edge]); edge++)
            if (domains [domain] != y_state)
                sorted [n++] = used_domains [edge];
            else
            {
                bits -= matrix_0 [rle_model->y_index];
                bits += matrix_1 [rle_model->y_index];
            }
      
        if (n > 1)
            qsort (sorted, n, sizeof (word_t), sort_asc_word);
    }

    bits = - log2 (rle_model->count [n] / (real_t) rle_model->total);
    if (used_domains && n && sorted [0] == 0)
    {
        word_t array0 [2] = {0, NO_EDGE};
        bits += qac_bits (array0, array0, level, y_state, wfa, rle_model->domain_0);
    }
    else
    {
        word_t array0 [2] = {NO_EDGE};
        bits += qac_bits (array0, array0, level, y_state, wfa, rle_model->domain_0);
    }
   
    last = 1;
    for (edge = 0; edge < n; edge++)
        if ((into = sorted [edge]) && rle_model->n - 1 - last)
        {
            bits += bits_bin_code (into - last, rle_model->n - 1 - last);
            last  = into + 1;
        }
   
    return bits;
}

static void
rle_update (const word_t *domains, const word_t *used_domains,
            unsigned level, int y_state, const wfa_t *wfa, void *model)
{
    rle_model_t *rle_model  = (rle_model_t *) model;
    bool_t   state_0    = NO, state_y = NO;
    word_t   array0 [2] = {0, NO_EDGE};
    unsigned     edge = 0;
   
    if (y_state >= 0 && !usedomain (y_state, wfa)) /* don't use y-state */
        y_state = -1;

    if (used_domains)
    {
        word_t   domain;
      
        for (edge = 0; isedge (domain = used_domains [edge]); edge++)
            if (domains [domain] == 0)
                state_0 = YES;
            else if (domains [domain] == y_state)
                state_y = YES;
    }
   
    rle_model->count [edge]++;
    rle_model->total++;

    qac_update (array0, array0 + (state_0 ? 0 : 1), level, y_state, wfa,
                rle_model->domain_0);

    if (state_y)
        rle_model->y_index >>= 1;
    else
        rle_model->y_index++;
    if (rle_model->y_index > 1020)   /* check for overflow */
        rle_model->y_index = 1020; 
}

static bool_t
rle_append (unsigned new_state, unsigned level, const wfa_t *wfa, void *model)
{
    rle_model_t *rle_model = (rle_model_t *) model; /* probability model */
   
    if (rle_model->n >= rle_model->max_domains)
        return NO;            /* don't use state in domain pool */
    else
    {
        rle_model->states [rle_model->n] = new_state;
        rle_model->n++;

        if (new_state == 0)
        {
            assert (rle_model->n == 1);
            qac_append (0, -1, wfa, rle_model->domain_0);
        }
      
        return YES;           /* state will be used in domain pool */
    }
}

static void
rle_chroma (unsigned max_domains, const wfa_t *wfa, void *model)
{
    rle_model_t *rle_model = (rle_model_t *) model; /* probability model */
   
    if (max_domains < rle_model->n)  /* choose most probable domains */
    {
        unsigned  n;
        word_t   *states  = Calloc (max_domains, sizeof (word_t));
        word_t   *domains = compute_hits (wfa->basis_states, wfa->states - 1,
                                          max_domains, wfa);
      
        for (n = 0; n < max_domains && domains [n] >= 0; n++)
            states [n] = domains [n];

        assert (states [0] == 0);
        max_domains = MIN(max_domains, n);
        Free (domains);

        Free (rle_model->states);
        rle_model->states = states;
        rle_model->n      = max_domains;
    }
    rle_model->y_index     = 0;
    rle_model->max_domains = rle_model->n;
}

/*****************************************************************************
         run length encoding pool no special chroma pool
*****************************************************************************/

static domain_pool_t *
alloc_rle_no_chroma_domain_pool (unsigned max_domains, unsigned max_edges,
                                 const wfa_t *wfa)
/*
 *  Domain pool with state images {0, ..., 'max_domains').
 *  Underlying probability model: rle 
 *  Domain pool is not changed for chroma bands
 */
{
    domain_pool_t *pool = alloc_rle_domain_pool (max_domains, max_edges, wfa);
   
    pool->chroma = default_chroma;

    return pool;
}

/*****************************************************************************
              default functions (see domain-pool.h)
*****************************************************************************/

static domain_pool_t *
default_alloc (void)
{
    domain_pool_t *pool;

    pool                  = Calloc (1, sizeof (domain_pool_t));
    pool->model           = default_model_alloc(0);
    pool->generate        = NULL;
    pool->bits            = NULL;
    pool->update          = default_update;
    pool->append          = default_append;
    pool->chroma          = default_chroma;
    pool->free            = default_free;
    pool->model_free      = default_model_free;
    pool->model_duplicate = default_model_duplicate;
   
    return pool;
}

static void *
default_model_duplicate (const void *src)
{
    return NULL;
}

static void *
default_model_alloc (unsigned max_domains)
{
    return NULL;
}

static void
default_model_free (void *model)
{
    if (model)
        Free (model);
}

static void
default_free (domain_pool_t *pool)
{
    pool->model_free (pool->model);
    Free (pool);
}

static void
default_update (const word_t *domains, const word_t *used_domains,
                unsigned level, int y_state, const wfa_t *wfa, void *model)
{
    return;              /* nothing to do */
}

static bool_t
default_append (unsigned new_state, unsigned level,
                const wfa_t *wfa, void *model)
{
    return YES;              /* use every state in lin comb */
}

static void
default_chroma (unsigned max_domains, const wfa_t *wfa, void *model)
{
    return;              /* don't alter domain pool */
}

static void
init_matrix_probabilities (void)
/*
 *  Compute the information contents of matrix element '0' and '1' for
 *  each possible probability index 0, ... ,  1023. These values are
 *  obtained from the probability model in the quasi arithmetic
 *  coding module.
 *
 *  No return value.
 *
 *  Side effects:
 *  local arrays matrix_0 and matrix_1 are initialized if not already done.
 */
{
    if (matrix_0 == NULL || matrix_1 == NULL)
    {
        unsigned index;           
        unsigned n, exp;
      
        matrix_0 = Calloc (1 << (MAX_PROB + 1), sizeof (real_t));
        matrix_1 = Calloc (1 << (MAX_PROB + 1), sizeof (real_t));
   
        for (index = 0, n = MIN_PROB; n <= MAX_PROB; n++)
            for (exp = 0; exp < (unsigned) 1 << n; exp++, index++)
            {
                matrix_1 [index] = -log2 (1 / (real_t) (1 << n));
                matrix_0 [index] = -log2 (1 - 1 / (real_t) (1 << n));
            }
    }
}