Debian Codegraph @

converter/other/fiasco/lib/arith.c

raw
1 /*
2 * arith.c: Adaptive arithmetic coding and decoding
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:49:37 $
12 * $Author: hafner $
13 * $Revision: 5.1 $
14 * $State: Exp $
15 */
16
17 #include "config.h"
18
19 #include "types.h"
20 #include "macros.h"
21 #include "error.h"
22
23 #include "bit-io.h"
24 #include "misc.h"
25 #include "arith.h"
26
27 /******************************************************************************
28
29 public code
30
31 ******************************************************************************/
32
33 arith_t *
34 alloc_encoder (bitfile_t *output)
35 /*
36 * Arithmetic coder constructor:
37 * Initialize the arithmetic coder.
38 *
39 * Return value:
40 * A pointer to the new coder structure
41 */
42 {
43 arith_t *arith = Calloc (1, sizeof (arith_t));
44
45 assert (output);
46
47 arith->low = LOW;
48 arith->high = HIGH;
49 arith->underflow = 0;
50 arith->file = output;
51
52 return arith;
53 }
54
55 void
56 free_encoder (arith_t *arith)
57 /*
58 * Arithmetic encoder destructor.
59 * Flush the arithmetic coder. Append all remaining bits to the
60 * output stream. Append zero bits to get the output file byte aligned.
61 *
62 * No return value.
63 */
64 {
65 u_word_t low; /* start of the current code range */
66 u_word_t high; /* end of the current code range */
67 u_word_t underflow; /* number of underflow bits pending */
68 bitfile_t *output;
69
70 assert (arith);
71
72 low = arith->low;
73 high = arith->high;
74 underflow = arith->underflow;
75 output = arith->file;
76
77 low = high;
78
79 RESCALE_OUTPUT_INTERVAL;
80
81 OUTPUT_BYTE_ALIGN (output);
82
83 Free (arith);
84 }
85
86 real_t
87 encode_symbol (unsigned symbol, arith_t *arith, model_t *model)
88 /*
89 * Encode the given 'symbol' using the given probability 'model'.
90 * The current state of the arithmetic coder is given by 'arith'.
91 * Output bits are appended to the stream 'output'.
92 *
93 * The model is updated after encoding the symbol (if necessary the
94 * symbol counts are rescaled).
95 *
96 * Return value:
97 * information content of the encoded symbol.
98 *
99 * Side effects:
100 * 'model' is updated (probability distribution)
101 * 'arith' is updated (coder state)
102 */
103 {
104 u_word_t low_count; /* lower bound of 'symbol' interval */
105 u_word_t high_count; /* upper bound of 'symbol' interval */
106 u_word_t scale; /* range of all 'm' symbol intervals */
107 unsigned range; /* range of current interval */
108 unsigned index; /* index of probability model */
109 u_word_t low; /* start of the current code range */
110 u_word_t high; /* end of the current code range */
111 u_word_t underflow; /* number of underflow bits pending */
112 bitfile_t *output; /* output file */
113
114 assert (model && arith);
115
116 /*
117 * Get interval values
118 */
119 low = arith->low;
120 high = arith->high;
121 underflow = arith->underflow;
122 output = arith->file;
123
124 assert (high > low);
125
126 if (model->order > 0) /* order-'n' model*/
127 {
128 unsigned power; /* multiplicator */
129 unsigned i;
130
131 /*
132 * Compute index of the probability model to use.
133 * See init_model() for more details.
134 */
135 power = 1; /* multiplicator */
136 index = 0; /* address of prob. model */
137
138 for (i = 0; i < model->order; i++) /* generate a M-nary number */
139 {
140 index += model->context [i] * power;
141 power *= model->symbols;
142 }
143
144 index *= model->symbols + 1; /* we need space for M + 1 elements */
145
146 for (i = 0; i < model->order - 1; i++)
147 model->context [i] = model->context [i + 1];
148 model->context [i] = symbol;
149 }
150 else
151 index = 0;
152
153 scale = model->totals [index + model->symbols];
154 low_count = model->totals [index + symbol];
155 high_count = model->totals [index + symbol + 1];
156
157 /*
158 * Compute the new interval depending on the input 'symbol'.
159 */
160 range = (high - low) + 1;
161 high = low + (u_word_t) ((range * high_count) / scale - 1);
162 low = low + (u_word_t) ((range * low_count) / scale);
163
164 RESCALE_OUTPUT_INTERVAL;
165
166 if (model->scale > 0) /* adaptive model */
167 {
168 unsigned i;
169
170 /*
171 * Update probability model
172 */
173 for (i = symbol + 1; i <= model->symbols; i++)
174 model->totals [index + i]++;
175 if (model->totals [index + model->symbols] > model->scale) /* scaling */
176 {
177 for (i = 1; i <= model->symbols; i++)
178 {
179 model->totals [index + i] >>= 1;
180 if (model->totals [index + i] <= model->totals [index + i - 1])
181 model->totals [index + i] = model->totals [index + i - 1] + 1;
182 }
183 }
184 }
185
186 /*
187 * Store interval values
188 */
189 arith->low = low;
190 arith->high = high;
191 arith->underflow = underflow;
192
193 return - log2 ((high_count - low_count) / (real_t) scale);
194 }
195
196 void
197 encode_array (bitfile_t *output, const unsigned *data, const unsigned *context,
198 const unsigned *c_symbols, unsigned n_context, unsigned n_data,
199 unsigned scaling)
200 /*
201 * Arithmetic coding of #'n_data' symbols given in the array 'data'.
202 * If 'n_context' > 1 then a number (context [n]) is assigned to every
203 * data element n, specifying which context (i.e. number of symbols given by
204 * c_symbols [context [n]] and adaptive probability model) must be used.
205 * Rescale probability models if range > 'scaling'.
206 *
207 * No return value.
208 */
209 {
210 u_word_t **totals; /* probability model */
211
212 if (!n_context)
213 n_context = 1; /* always use one context */
214
215 assert (output && c_symbols && data);
216 assert (n_context == 1 || context);
217
218 /*
219 * Allocate probability models, start with uniform distribution
220 */
221 totals = Calloc (n_context, sizeof (u_word_t *));
222 {
223 unsigned c;
224
225 for (c = 0; c < n_context; c++)
226 {
227 unsigned i;
228
229 totals [c] = Calloc (c_symbols [c] + 1, sizeof (u_word_t));
230 totals [c][0] = 0;
231
232 for (i = 0; i < c_symbols [c]; i++)
233 totals [c][i + 1] = totals [c][i] + 1;
234 }
235 }
236
237 /*
238 * Encode array elements
239 */
240 {
241 u_word_t low = 0; /* Start of the current code range */
242 u_word_t high = 0xffff; /* End of the current code range */
243 u_word_t underflow = 0; /* Number of underflow bits pending */
244 unsigned n;
245
246 for (n = 0; n < n_data; n++)
247 {
248 u_word_t low_count; /* lower bound of 'symbol' interval */
249 u_word_t high_count; /* upper bound of 'symbol' interval */
250 u_word_t scale; /* range of all 'm' symbol intervals */
251 unsigned range; /* current range */
252 int d; /* current data symbol */
253 int c; /* context of current data symbol */
254
255 d = data [n];
256 c = n_context > 1 ? context [n] : 0;
257
258 scale = totals [c][c_symbols [c]];
259 low_count = totals [c][d];
260 high_count = totals [c][d + 1];
261
262 /*
263 * Rescale high and low for the new symbol.
264 */
265 range = (high - low) + 1;
266 high = low + (u_word_t) ((range * high_count) / scale - 1);
267 low = low + (u_word_t) ((range * low_count) / scale);
268 RESCALE_OUTPUT_INTERVAL;
269
270 /*
271 * Update probability models
272 */
273 {
274 unsigned i;
275
276 for (i = d + 1; i < c_symbols [c] + 1; i++)
277 totals [c][i]++;
278
279 if (totals [c][c_symbols [c]] > scaling) /* scaling */
280 for (i = 1; i < c_symbols [c] + 1; i++)
281 {
282 totals [c][i] >>= 1;
283 if (totals [c][i] <= totals [c][i - 1])
284 totals [c][i] = totals [c][i - 1] + 1;
285 }
286 }
287 }
288 /*
289 * Flush arithmetic encoder
290 */
291 low = high;
292 RESCALE_OUTPUT_INTERVAL;
293 OUTPUT_BYTE_ALIGN (output);
294 }
295
296 /*
297 * Cleanup ...
298 */
299 {
300 unsigned c;
301 for (c = 0; c < n_context; c++)
302 Free (totals [c]);
303 Free (totals);
304 }
305 }
306
307 arith_t *
308 alloc_decoder (bitfile_t *input)
309 /*
310 * Arithmetic decoder constructor:
311 * Initialize the arithmetic decoder with the first
312 * 16 input bits from the stream 'input'.
313 *
314 * Return value:
315 * A pointer to the new decoder structure
316 */
317
318 {
319 arith_t *arith = Calloc (1, sizeof (arith_t));
320
321 assert (input);
322
323 arith->low = LOW;
324 arith->high = HIGH;
325 arith->code = get_bits (input, 16);
326 arith->file = input;
327
328 return arith;
329 }
330
331 void
332 free_decoder (arith_t *arith)
333 /*
334 * Arithmetic decoder destructor:
335 * Flush the arithmetic decoder, i.e., read bits to get the input
336 * file byte aligned.
337 *
338 * No return value.
339 *
340 * Side effects:
341 * structure 'arith' is discarded.
342 */
343 {
344 assert (arith);
345
346 INPUT_BYTE_ALIGN (arith->file);
347
348 Free (arith);
349 }
350
351 unsigned
352 decode_symbol (arith_t *arith, model_t *model)
353 /*
354 * Decode the next symbol - the state of the arithmetic decoder
355 * is given in 'arith'. Read refinement bits from the stream 'input'
356 * and use the given probability 'model'. Update the probability model after
357 * decoding the symbol (if necessary also rescale the symbol counts).
358 *
359 * Return value:
360 * decoded symbol
361 *
362 * Side effects:
363 * 'model' is updated (probability distribution)
364 * 'arith' is updated (decoder state)
365 */
366 {
367 unsigned range; /* range of current interval */
368 unsigned count; /* value in the current interval */
369 unsigned index; /* index of probability model */
370 unsigned symbol; /* decoded symbol */
371 u_word_t scale; /* range of all 'm' symbol intervals */
372 u_word_t low; /* start of the current code range */
373 u_word_t high; /* end of the current code range */
374 u_word_t code; /* the present input code value */
375 bitfile_t *input; /* input file */
376
377 assert (arith && model);
378
379 /*
380 * Get interval values
381 */
382 low = arith->low;
383 high = arith->high;
384 code = arith->code;
385 input = arith->file;
386
387 assert (high > low);
388
389 if (model->order > 0) /* order-'n' model */
390 {
391 unsigned power; /* multiplicator */
392 unsigned i;
393
394 /*
395 * Compute index of the probability model to use.
396 * See init_model() for more details.
397 */
398 power = 1; /* multiplicator */
399 index = 0; /* address of prob. model */
400
401 for (i = 0; i < model->order; i++) /* generate a m-nary number */
402 {
403 index += model->context[i] * power;
404 power *= model->symbols;
405 }
406
407 index *= model->symbols + 1; /* we need space for m + 1 elements */
408 }
409 else
410 index = 0;
411
412 scale = model->totals [index + model->symbols];
413 range = (high - low) + 1;
414 count = ((code - low + 1) * scale - 1) / range;
415
416 for (symbol = model->symbols; count < model->totals [index + symbol];
417 symbol--)
418 ;
419
420 if (model->order > 0) /* order-'n' model */
421 {
422 unsigned i;
423
424 for (i = 0; i < model->order - 1; i++)
425 model->context [i] = model->context [i + 1];
426 model->context [i] = symbol;
427 }
428
429 /*
430 * Compute interval boundaries
431 */
432 {
433 u_word_t low_count; /* lower bound of 'symbol' interval */
434 u_word_t high_count; /* upper bound of 'symbol' interval */
435
436 low_count = model->totals [index + symbol];
437 high_count = model->totals [index + symbol + 1];
438 high = low + (u_word_t) ((range * high_count) / scale - 1 );
439 low = low + (u_word_t) ((range * low_count) / scale );
440 }
441
442 RESCALE_INPUT_INTERVAL;
443
444 if (model->scale > 0) /* adaptive model */
445 {
446 unsigned i;
447
448 /*
449 * Update probability model
450 */
451 for (i = symbol + 1; i <= model->symbols; i++)
452 model->totals [index + i]++;
453 if (model->totals [index + model->symbols] > model->scale) /* scaling */
454 {
455 for (i = 1; i <= model->symbols; i++)
456 {
457 model->totals [index + i] >>= 1;
458 if (model->totals [index + i] <= model->totals [index + i - 1])
459 model->totals [index + i] = model->totals [index + i - 1] + 1;
460 }
461 }
462 }
463
464 /*
465 * Store interval values
466 */
467 arith->low = low;
468 arith->high = high;
469 arith->code = code;
470
471 return symbol;
472 }
473
474 unsigned *
475 decode_array (bitfile_t *input, const unsigned *context,
476 const unsigned *c_symbols, unsigned n_context,
477 unsigned n_data, unsigned scaling)
478 /*
479 * Arithmetic decoding of #'n_data' symbols.
480 * If 'n_context' > 1 then a number (context [n]) is assigned to every
481 * data element n, specifying which context (i.e. number of symbols given by
482 * c_symbols [context [n]] and adaptive probability model) must be used.
483 * Rescale probability models if range > 'scaling'.
484 *
485 * Return value:
486 * pointer to array containing the decoded symbols
487 */
488 {
489 unsigned *data; /* array to store decoded symbols */
490 u_word_t **totals; /* probability model */
491
492 if (n_context < 1)
493 n_context = 1; /* always use one context */
494 assert (input && c_symbols);
495 assert (n_context == 1 || context);
496
497 data = Calloc (n_data, sizeof (unsigned));
498
499 /*
500 * Allocate probability models, start with uniform distribution
501 */
502 totals = Calloc (n_context, sizeof (u_word_t *));
503 {
504 unsigned c;
505
506 for (c = 0; c < n_context; c++)
507 {
508 unsigned i;
509
510 totals [c] = Calloc (c_symbols [c] + 1, sizeof (u_word_t));
511 totals [c][0] = 0;
512
513 for (i = 0; i < c_symbols [c]; i++)
514 totals [c][i + 1] = totals [c][i] + 1;
515 }
516 }
517
518 /*
519 * Fill array 'data' with decoded values
520 */
521 {
522 u_word_t code = get_bits (input, 16); /* The present input code value */
523 u_word_t low = 0; /* Start of the current code range */
524 u_word_t high = 0xffff; /* End of the current code range */
525 unsigned n;
526
527 for (n = 0; n < n_data; n++)
528 {
529 u_word_t scale; /* range of all 'm' symbol intervals */
530 u_word_t low_count; /* lower bound of 'symbol' interval */
531 u_word_t high_count; /* upper bound of 'symbol' interval */
532 unsigned count; /* value in the current interval */
533 unsigned range; /* current interval range */
534 unsigned d; /* current data symbol */
535 unsigned c; /* context of current data symbol */
536
537 c = n_context > 1 ? context [n] : 0;
538
539 assert (high > low);
540 scale = totals [c][c_symbols [c]];
541 range = (high - low) + 1;
542 count = (((code - low) + 1 ) * scale - 1) / range;
543
544 for (d = c_symbols [c]; count < totals [c][d]; d--) /* next symbol */
545 ;
546 low_count = totals [c][d];
547 high_count = totals [c][d + 1];
548
549 high = low + (u_word_t) ((range * high_count) / scale - 1 );
550 low = low + (u_word_t) ((range * low_count) / scale );
551 RESCALE_INPUT_INTERVAL;
552
553 /*
554 * Updata probability models
555 */
556 {
557 unsigned i;
558
559 for (i = d + 1; i < c_symbols [c] + 1; i++)
560 totals [c][i]++;
561
562 if (totals [c][c_symbols [c]] > scaling) /* scaling */
563 for (i = 1; i < c_symbols [c] + 1; i++)
564 {
565 totals [c][i] >>= 1;
566 if (totals [c][i] <= totals [c][i - 1])
567 totals [c][i] = totals [c][i - 1] + 1;
568 }
569 }
570 data [n] = d;
571 }
572 INPUT_BYTE_ALIGN (input);
573 }
574
575 /*
576 * Cleanup ...
577 */
578 {
579 unsigned c;
580
581 for (c = 0; c < n_context; c++)
582 Free (totals [c]);
583 Free (totals);
584 }
585
586 return data;
587 }
588
589 model_t *
590 alloc_model (unsigned m, unsigned scale, unsigned n, unsigned *totals)
591 /*
592 * Model constructor:
593 * allocate and initialize an order-'n' probability model.
594 * The size of the source alphabet is 'm'. Rescale the symbol counts after
595 * 'scale' symbols are encoded/decoded. The initial probability of every
596 * symbol is 1/m.
597 * If 'scale' = 0 then use static modeling (p = 1/n).
598 * If 'totals' is not NULL then use this array of 'm' values to set
599 * the initial counts.
600 *
601 * Return value:
602 * a pointer to the new probability model structure.
603 *
604 * Note: We recommend a small size of the alphabet because no escape codes
605 * are used to encode/decode previously unseen symbols.
606 *
607 */
608 {
609 model_t *model; /* new probability model */
610 unsigned num; /* number of contexts to allocate */
611 bool_t cont; /* continue flag */
612 bool_t dec; /* next order flag */
613 unsigned i;
614
615 /*
616 * Allocate memory for the structure
617 */
618 model = Calloc (1, sizeof (model_t));
619 model->symbols = m;
620 model->scale = scale;
621 model->order = n;
622 model->context = n > 0 ? Calloc (n, sizeof (unsigned)) : NULL;
623 /*
624 * Allocate memory for the probability model.
625 * Each of the m^n different contexts requires its own probability model.
626 */
627 for (num = 1, i = 0; i < model->order; i++)
628 num *= model->symbols;
629
630 model->totals = Calloc (num * (model->symbols + 1), sizeof (unsigned));
631
632 for (i = 0; i < model->order; i++)
633 model->context[i] = 0; /* start with context 0,0, .. ,0 */
634 cont = YES;
635 while (cont) /* repeat while context != M ... M */
636 {
637 int power; /* multiplicator */
638 int index; /* index of probability model */
639 /*
640 * There are m^n different contexts:
641 * Let "context_1 context_2 ... context_n symbol" be the current input
642 * stream then the index of the probability model is given by:
643 * index = context_1 * M^0 + context_2 * M^1 + ... + context_n * M^(n-1)
644 */
645 power = 1; /* multiplicator */
646 index = 0; /* address of prob. model */
647
648 for (i = 0; i < model->order; i++) /* generate a m-nary number */
649 {
650 index += model->context[i] * power;
651 power *= model->symbols;
652 }
653
654 index *= model->symbols + 1; /* size of each model is m + 1 */
655
656 model->totals [index + 0] = 0; /* always zero */
657
658 for (i = 1; i <= model->symbols; i++) /* prob of each symbol is 1/m or
659 as given in totals */
660 model->totals[index + i] = model->totals [index + i - 1]
661 + (totals ? totals [i - 1] : 1);
662
663 if (model->order == 0) /* order-0 model */
664 cont = NO;
665 else /* try next context */
666 for (i = model->order - 1, dec = YES; dec; i--)
667 {
668 dec = NO;
669 model->context[i]++;
670 if (model->context[i] >= model->symbols)
671 {
672 /* change previous context */
673 model->context[i] = 0;
674 if (i > 0) /* there's still a context remaining */
675 dec = YES;
676 else
677 cont = NO; /* all context models initialized */
678 }
679 }
680 }
681 for (i = 0; i < model->order; i++)
682 model->context[i] = 0; /* start with context 0,0, .. ,0 */
683
684 return model;
685 }
686
687 void
688 free_model (model_t *model)
689 /*
690 * Model destructor:
691 * Free memory allocated by the arithmetic 'model'.
692 *
693 * No return value.
694 *
695 * Side effects:
696 * struct 'model' is discarded
697 */
698 {
699 if (model != NULL)
700 {
701 if (model->context != NULL)
702 Free (model->context);
703 Free (model->totals);
704 Free (model);
705 }
706 else
707 warning ("Can't free model <NULL>.");
708 }