README 7 KB
Newer Older
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
1 2 3
**********************************************************************
 libaec - Adaptive Entropy Coding library
**********************************************************************
4

5
Libaec provides fast lossless compression of 1 up to 32 bit wide
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
6 7 8 9 10 11
signed or unsigned integers (samples). The library achieves best
results for low entropy data as often encountered in space imaging
instrument data or numerical model output from weather or climate
simulations. While floating point representations are not directly
supported, they can also be efficiently coded by grouping exponents
and mantissa.
12

13
Libaec implements Golomb Rice coding as defined in the Space Data
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
14
System Standard document 121.0-B-2 [1], [2].
15

16

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
17 18 19
**********************************************************************
 Patents
**********************************************************************
20

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
21 22 23 24 25 26 27 28
In doc/license.txt a clarification on potentially applying
intellectual property rights is given.


**********************************************************************
 Installation
**********************************************************************

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
29
See INSTALL for details.
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49


**********************************************************************
 Encoding
**********************************************************************

In this context efficiency refers to the size of the encoded
data. Performance refers to the time it takes to encode data.

Suppose you have an array of 32 bit signed integers you want to
compress. The pointer pointing to the data shall be called *source,
output goes into *dest.

#include <libaec.h>

...
    struct aec_stream strm;
    int32_t *source;
    unsigned char *dest;

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
50
    /* input data is 32 bits wide */
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    strm.bits_per_sample = 32;

    /* define a block size of 16 */
    strm.block_size = 16;

    /* the reference sample interval is set to 128 blocks */
    strm.rsi = 128;

    /* input data is signed and needs to be preprocessed */
    strm.flags = AEC_DATA_SIGNED | AEC_DATA_PREPROCESS;

    /* pointer to input */
    strm.next_in = (unsigned char *)source;

    /* length of input in bytes */
66
    strm.avail_in = source_length * sizeof(int32_t);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
67 68

    /* pointer to output buffer */
69
    strm.next_out = dest;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
70 71

    /* length of output buffer in bytes */
72
    strm.avail_out = dest_length;
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

    /* initialize encoding */
    if (aec_encode_init(&strm) != AEC_OK)
        return 1;

    /* Perform encoding in one call and flush output. */
    /* In this example you must be sure that the output */
    /* buffer is large enough for all compressed output */
    if (aec_encode(&strm, AEC_FLUSH) != AEC_OK)
        return 1;

    /* free all resources used by encoder */
    aec_encode_end(&strm);
...

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
88 89 90 91
block_size can vary from 8 to 64 samples. Smaller blocks allow the
compression to adapt to rapid changes in entropy. Larger blocks create
less overhead but can be less efficient if entropy changes across the
block.
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
92 93

rsi sets the reference sample interval. A large RSI will improve
94 95
performance and efficiency. It will also increase memory requirements
since internal buffering is based on RSI size. A smaller RSI may be
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
96 97 98 99 100 101 102 103
desirable in situations where each RSI will be packetized and possible
error propagation has to be minimized (e.g. on board a spacecraft[2]).

Flags:

AEC_DATA_SIGNED: input data are signed integers. Specifying this
correctly increases compression efficiency. Default is unsigned.

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
104 105 106 107
AEC_DATA_PREPROCESS: preprocessing input will improve compression
efficiency if data samples are correlated. It will only cost
performance for no gain in efficiency if the data is already
uncorrelated.
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
108 109 110 111 112 113 114 115 116 117 118

AEC_DATA_MSB: input data is stored most significant byte first
i.e. big endian. You have to specify AEC_DATA_MSB even if your host
architecture is big endian. Default is little endian on all
architectures.

AEC_DATA_3BYTE: the 24 bit input data is stored in three bytes.

Data size:

Except for the AEC_DATA_3BYTE case for 24 bit data, the following
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
119 120
rules apply for deducing storage size from sample size
(bits_per_sample):
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
121

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
122
sample size   storage size
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
123 124 125 126
 1 -  8 bit   1 byte
 9 - 16 bit   2 bytes
17 - 32 bit   4 bytes (also for 24bit if AEC_DATA_3BYTE is not set)

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
127
If you use less bits than the storage size provides, then you have to
128
make sure that unused bits are not set. Libaec does not check this for
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
129 130 131
performance reasons and will produce undefined output if unused bits
are set.

132 133 134
Libaec accesses next_in and next_out buffers only bytewise. There are
no alignment requirements for these buffers.

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
135 136 137 138 139
Flushing:

aec_encode can be used in a streaming fashion by chunking input and
output and specifying AEC_NO_FLUSH. The function will return if either
the input runs empty or the output buffer is full. The calling
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
140 141 142 143
function can check avail_in and avail_out to see what occcurred. The
last call to aec_encode() must set AEC_FLUSH to drain all
output. aec.c is an example of streaming usage of encoding and
decoding.
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
144

145 146 147 148 149 150 151 152
Output:

Encoded data will be written to the buffer submitted with
next_out. The length of the compressed data is total_out.

See libaec.h for a detailed description of all relevant structure
members and constants.

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
153 154 155 156 157

**********************************************************************
 Decoding
**********************************************************************

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
158 159
Using decoding is very similar to encoding, only the meaning of input
and output is reversed.
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

#include <libaec.h>

...
    struct aec_stream strm;
    /* this is now the compressed data */
    unsigned char *source;
    /* here goes the uncompressed result */
    int32_t *dest;

    strm.bits_per_sample = 32;
    strm.block_size = 16;
    strm.rsi = 128;
    strm.flags = AEC_DATA_SIGNED | AEC_DATA_PREPROCESS;
    strm.next_in = source;
    strm.avail_in = source_length;
    strm.next_out = (unsigned char *)dest;
177
    strm.avail_out = dest_lenth * sizeof(int32_t);
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
178 179 180 181 182 183 184 185 186
    if (aec_decode_init(&strm) != AEC_OK)
        return 1;
    if (aec_decode(&strm, AEC_FLUSH) != AEC_OK)
        return 1;
    aec_decode_end(&strm);
...

It is essential for decoding that parameters like bits_per_sample,
block_size, rsi, and flags are exactly the same as they were for
187
encoding. Libaec does not store these parameters in the coded stream
Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
188 189 190
so it is up to the calling program to keep the correct parameters
between encoding and decoding.

191 192 193 194
The actual values of coding parameters are in fact only relevant for
efficiency and performance. Data integrity only depends on consistency
of the parameters.

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
195 196 197 198 199 200

**********************************************************************
 References
**********************************************************************

[1] Consultative Committee for Space Data Systems. Lossless Data
201 202
Compression. Recommendation for Space Data System Standards, CCSDS
121.0-B-2. Blue Book. Issue 2. Washington, D.C.: CCSDS, May 2012.
203 204
http://public.ccsds.org/publications/archive/121x0b2.pdf

Mathis Rosenhauer's avatar
Mathis Rosenhauer committed
205
[2] Consultative Committee for Space Data Systems. Lossless Data
206 207 208
Compression.  Recommendation for Space Data System Standards, CCSDS
120.0-G-2. Green Book. Issue 2. Washington, D.C.: CCSDS, December
2006.
209
http://public.ccsds.org/publications/archive/120x0g2.pdf