| 1 |
/* |
| 2 |
* arith.h |
| 3 |
* |
| 4 |
* This file is part of FIASCO (Fractal Image And Sequence COdec) |
| 5 |
* Copyright (C) 1994-2000 Ullrich Hafner |
| 6 |
*/ |
| 7 |
|
| 8 |
/* |
| 9 |
* $Date: 2000/06/14 20:49:37 $ |
| 10 |
* $Author: hafner $ |
| 11 |
* $Revision: 5.1 $ |
| 12 |
* $State: Exp $ |
| 13 |
*/ |
| 14 |
|
| 15 |
#ifndef _ARITH_H |
| 16 |
#define _ARITH_H |
| 17 |
|
| 18 |
#include "types.h" |
| 19 |
#include "bit-io.h" |
| 20 |
|
| 21 |
typedef struct model |
| 22 |
{ |
| 23 |
unsigned symbols; /* number of symbols in the alphabet */ |
| 24 |
unsigned scale; /* if totals > scale rescale totals */ |
| 25 |
unsigned order; /* order of the probability model */ |
| 26 |
unsigned *context; /* context of the model */ |
| 27 |
unsigned *totals; /* the totals */ |
| 28 |
} model_t; |
| 29 |
|
| 30 |
typedef struct arith |
| 31 |
{ |
| 32 |
u_word_t low; /* start of the current code range */ |
| 33 |
u_word_t high; /* end of the current code range */ |
| 34 |
u_word_t underflow; /* number of underflow bits pending */ |
| 35 |
u_word_t code; /* the present input code value */ |
| 36 |
bitfile_t *file; /* I/O stream */ |
| 37 |
} arith_t; |
| 38 |
|
| 39 |
enum interval {LOW = 0x0000, FIRST_QUARTER = 0x4000, HALF = 0x8000, |
| 40 |
THIRD_QUARTER = 0xc000, HIGH = 0xffff}; |
| 41 |
|
| 42 |
arith_t * |
| 43 |
alloc_encoder (bitfile_t *file); |
| 44 |
void |
| 45 |
free_encoder (arith_t *arith); |
| 46 |
real_t |
| 47 |
encode_symbol (unsigned symbol, arith_t *arith, model_t *model); |
| 48 |
void |
| 49 |
encode_array (bitfile_t *output, const unsigned *data, const unsigned *context, |
| 50 |
const unsigned *c_symbols, unsigned n_context, unsigned n_data, |
| 51 |
unsigned scaling); |
| 52 |
arith_t * |
| 53 |
alloc_decoder (bitfile_t *input); |
| 54 |
void |
| 55 |
free_decoder (arith_t *arith); |
| 56 |
unsigned |
| 57 |
decode_symbol (arith_t *arith, model_t *model); |
| 58 |
unsigned * |
| 59 |
decode_array (bitfile_t *input, const unsigned *context, |
| 60 |
const unsigned *c_symbols, unsigned n_context, |
| 61 |
unsigned n_data, unsigned scaling); |
| 62 |
model_t * |
| 63 |
alloc_model (unsigned m, unsigned scale, unsigned n, unsigned *totals); |
| 64 |
void |
| 65 |
free_model (model_t *model); |
| 66 |
|
| 67 |
#define RESCALE_INPUT_INTERVAL for (;;) \ |
| 68 |
if ((high >= HALF) && (low < HALF) && \ |
| 69 |
((low & FIRST_QUARTER) != FIRST_QUARTER \ |
| 70 |
|| (high & FIRST_QUARTER) != 0)) \ |
| 71 |
{ \ |
| 72 |
break; \ |
| 73 |
} \ |
| 74 |
else if ((high < HALF) || (low >= HALF)) \ |
| 75 |
{ \ |
| 76 |
low <<= 1; \ |
| 77 |
high <<= 1; \ |
| 78 |
high |= 1; \ |
| 79 |
code <<= 1; \ |
| 80 |
code += get_bit (input); \ |
| 81 |
} \ |
| 82 |
else \ |
| 83 |
{ \ |
| 84 |
code ^= FIRST_QUARTER; \ |
| 85 |
low &= FIRST_QUARTER - 1; \ |
| 86 |
low <<= 1; \ |
| 87 |
high <<= 1; \ |
| 88 |
high |= HALF + 1; \ |
| 89 |
code <<= 1; \ |
| 90 |
code += get_bit (input); \ |
| 91 |
} |
| 92 |
|
| 93 |
#define RESCALE_OUTPUT_INTERVAL for (;;) \ |
| 94 |
{ \ |
| 95 |
if (high < HALF) \ |
| 96 |
{ \ |
| 97 |
put_bit (output, 0); \ |
| 98 |
for (; underflow; underflow--) \ |
| 99 |
put_bit (output, 1); \ |
| 100 |
} \ |
| 101 |
else if (low >= HALF) \ |
| 102 |
{ \ |
| 103 |
put_bit (output, 1); \ |
| 104 |
for (; underflow; underflow--) \ |
| 105 |
put_bit (output, 0); \ |
| 106 |
} \ |
| 107 |
else if (high < THIRD_QUARTER && \ |
| 108 |
low >= FIRST_QUARTER) \ |
| 109 |
{ \ |
| 110 |
underflow++; \ |
| 111 |
high |= FIRST_QUARTER; \ |
| 112 |
low &= FIRST_QUARTER - 1; \ |
| 113 |
} \ |
| 114 |
else \ |
| 115 |
break; \ |
| 116 |
high <<= 1; \ |
| 117 |
high |= 1; \ |
| 118 |
low <<= 1; \ |
| 119 |
} |
| 120 |
|
| 121 |
#endif /* not _ARITH_H */ |
| 122 |
|