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
|
/*
* arith.h
*
* This file is part of FIASCO (Fractal Image And Sequence COdec)
* Copyright (C) 1994-2000 Ullrich Hafner
*/
/*
* $Date: 2000/06/14 20:49:37 $
* $Author: hafner $
* $Revision: 5.1 $
* $State: Exp $
*/
#ifndef _ARITH_H
#define _ARITH_H
#include "types.h"
#include "bit-io.h"
typedef struct model
{
unsigned symbols; /* number of symbols in the alphabet */
unsigned scale; /* if totals > scale rescale totals */
unsigned order; /* order of the probability model */
unsigned *context; /* context of the model */
unsigned *totals; /* the totals */
} model_t;
typedef struct arith
{
u_word_t low; /* start of the current code range */
u_word_t high; /* end of the current code range */
u_word_t underflow; /* number of underflow bits pending */
u_word_t code; /* the present input code value */
bitfile_t *file; /* I/O stream */
} arith_t;
enum interval {LOW = 0x0000, FIRST_QUARTER = 0x4000, HALF = 0x8000,
THIRD_QUARTER = 0xc000, HIGH = 0xffff};
arith_t *
alloc_encoder (bitfile_t *file);
void
free_encoder (arith_t *arith);
real_t
encode_symbol (unsigned symbol, arith_t *arith, model_t *model);
void
encode_array (bitfile_t *output, const unsigned *data, const unsigned *context,
const unsigned *c_symbols, unsigned n_context, unsigned n_data,
unsigned scaling);
arith_t *
alloc_decoder (bitfile_t *input);
void
free_decoder (arith_t *arith);
unsigned
decode_symbol (arith_t *arith, model_t *model);
unsigned *
decode_array (bitfile_t *input, const unsigned *context,
const unsigned *c_symbols, unsigned n_context,
unsigned n_data, unsigned scaling);
model_t *
alloc_model (unsigned m, unsigned scale, unsigned n, unsigned *totals);
void
free_model (model_t *model);
#define RESCALE_INPUT_INTERVAL for (;;) \
if ((high >= HALF) && (low < HALF) && \
((low & FIRST_QUARTER) != FIRST_QUARTER \
|| (high & FIRST_QUARTER) != 0)) \
{ \
break; \
} \
else if ((high < HALF) || (low >= HALF)) \
{ \
low <<= 1; \
high <<= 1; \
high |= 1; \
code <<= 1; \
code += get_bit (input); \
} \
else \
{ \
code ^= FIRST_QUARTER; \
low &= FIRST_QUARTER - 1; \
low <<= 1; \
high <<= 1; \
high |= HALF + 1; \
code <<= 1; \
code += get_bit (input); \
}
#define RESCALE_OUTPUT_INTERVAL for (;;) \
{ \
if (high < HALF) \
{ \
put_bit (output, 0); \
for (; underflow; underflow--) \
put_bit (output, 1); \
} \
else if (low >= HALF) \
{ \
put_bit (output, 1); \
for (; underflow; underflow--) \
put_bit (output, 0); \
} \
else if (high < THIRD_QUARTER && \
low >= FIRST_QUARTER) \
{ \
underflow++; \
high |= FIRST_QUARTER; \
low &= FIRST_QUARTER - 1; \
} \
else \
break; \
high <<= 1; \
high |= 1; \
low <<= 1; \
}
#endif /* not _ARITH_H */
|