1 /* Adaptive Entropy Decoder */
2 /* CCSDS 121.0-B-1 and CCSDS 120.0-G-2 */
14 while (state->bitp < (unsigned)(n)) \
16 if (strm->avail_in == 0) goto req_buffer; \
20 state->acc |= (uint64_t)(*strm->next_in++); \
26 ((state->acc >> (state->bitp - (n))) & ((1ULL << (n)) - 1))
30 state->bitp -= (unsigned)(n); \
37 while (GET(state->fs + 1) == 0) \
49 DROP(state->fs + 1); \
52 #define REFBLOCK(strm) (strm->pp && (strm->total_out / strm->block_size) \
53 % strm->segment_size == 0)
56 typedef struct internal_state {
57 uint32_t id_len; /* bit length of code option identification key */
58 int *id_table; /* table maps IDs to states */
59 size_t ref_int; /* reference sample is every ref_int samples */
60 uint32_t last_out; /* previous output for post-processing */
61 int64_t xmin; /* minimum integer for post-processing */
62 int64_t xmax; /* maximum integer for post-processing */
63 int mode; /* current mode of FSM */
64 int pushed_mode; /* originating mode for generic modes */
65 size_t count, i; /* total number of samples in block and current sample */
66 int k; /* k for split-sample options */
67 uint32_t *block; /* block buffer for split-sample options */
68 uint64_t acc; /* accumulator for currently used bit sequence */
69 uint8_t bitp; /* bit pointer to the next unused bit in accumulator */
70 uint32_t fs; /* last fundamental sequence in accumulator */
71 uint32_t delta1; /* interim result we need to keep for SE option */
74 /* decoding table for the second-extension option */
75 static const uint32_t second_extension[36][2] = {
78 {2, 3}, {2, 3}, {2, 3},
79 {3, 6}, {3, 6}, {3, 6}, {3, 6},
80 {4, 10}, {4, 10}, {4, 10}, {4, 10}, {4, 10},
81 {5, 15}, {5, 15}, {5, 15}, {5, 15}, {5, 15}, {5, 15},
82 {6, 21}, {6, 21}, {6, 21}, {6, 21}, {6, 21}, {6, 21}, {6, 21},
83 {7, 28}, {7, 28}, {7, 28}, {7, 28}, {7, 28}, {7, 28}, {7, 28}, {7, 28}
107 static inline int output(ae_streamp strm, uint32_t out)
110 Outputs a post-processed sample.
112 If no post-processor is present then output unaltered.
118 if (strm->avail_out == 0)
120 return AE_STREAM_END;
124 if (strm->pp && (strm->total_out % state->ref_int != 0))
129 if ((x - state->xmin) < (state->xmax - x))
131 th = x - state->xmin;
135 th = state->xmax - x;
152 *strm->next_out++ = state->last_out = out;
158 int ae_decode_init(ae_streamp strm)
163 /* Some sanity checks */
164 if (strm->bit_per_sample > 32 || strm->bit_per_sample == 0)
169 /* Internal state for decoder */
170 state = (decode_state *) malloc(sizeof(decode_state));
177 if (16 < strm->bit_per_sample)
179 else if (8 < strm->bit_per_sample)
184 state->ref_int = strm->block_size * strm->segment_size;
186 modi = 1UL << state->id_len;
187 state->id_table = (int *)malloc(modi * sizeof(int));
188 if (state->id_table == NULL)
192 state->id_table[0] = M_LOW_ENTROPY;
193 for (i = 1; i < modi - 1; i++)
195 state->id_table[i] = M_SPLIT;
197 state->id_table[modi - 1] = M_UNCOMP;
199 state->block = (uint32_t *)malloc(strm->block_size * sizeof(uint32_t));
200 if (state->block == NULL)
207 state->xmax = (1ULL << strm->bit_per_sample) - 1;
214 int ae_decode(ae_streamp strm, int flush)
218 uint32_t gamma, beta, ms;
225 /* Slow but restartable finite-state machine implementation
226 of the adaptive entropy decoder. Can work with one byte
227 input und one sample output buffers. Inspired by zlib.
229 TODO: Fast version with prior buffer size checking.
230 Flush modes like in zlib
238 state->mode = state->id_table[id];
243 state->count = strm->block_size;
245 state->mode = M_SPLIT_FS;
248 state->pushed_mode = M_SPLIT_FS;
249 state->mode = M_SAMPLE_GET;
255 while(state->i < state->count)
258 state->block[state->i] = GETFS() << state->k;
263 state->mode = M_SPLIT_BITS;
266 while(state->i < state->count)
269 state->block[state->i] |= GET(state->k);
274 state->mode = M_SPLIT_OUTPUT;
277 while(state->i < state->count)
279 if (output(strm, state->block[state->i]) == AE_OK)
299 state->mode = M_ZERO_BLOCK;
304 state->pushed_mode = state->mode;
305 state->mode = M_SAMPLE_GET;
311 zero_blocks = GETFS() + 1;
313 if (zero_blocks == ROS)
315 zero_blocks = strm->segment_size - (
316 (strm->total_out / strm->block_size)
317 % strm->segment_size);
319 state->count = zero_blocks * strm->block_size;
324 state->mode = M_ZERO_OUTPUT;
327 while(state->count > 0 && output(strm, 0) == AE_OK)
331 if (state->count == 0)
342 state->count = strm->bit_per_sample / 2;
343 state->mode = M_SE_DECODE;
349 state->mode = M_GAMMA_GET;
358 state->mode = M_GAMMA_OUTPUT_0;
360 case M_GAMMA_OUTPUT_0:
362 beta = second_extension[gamma][0];
363 ms = second_extension[gamma][1];
364 state->delta1 = gamma - ms;
365 if (!(REFBLOCK(strm) && state->count == strm->bit_per_sample / 2 - 1))
367 if (output(strm, beta - state->delta1) != AE_OK)
371 state->mode = M_GAMMA_OUTPUT_1;
373 case M_GAMMA_OUTPUT_1:
374 if (output(strm, state->delta1) != AE_OK)
377 state->mode = M_SE_DECODE;
381 state->count = strm->block_size;
382 state->mode = M_UNCOMP_COPY;
388 state->pushed_mode = M_UNCOMP_COPY;
389 state->mode = M_SAMPLE_GET;
398 ASK(strm->bit_per_sample);
399 state->mode = M_SAMPLE_OUTPUT;
401 case M_SAMPLE_OUTPUT:
402 if (output(strm, GET(strm->bit_per_sample)) == AE_OK)
404 DROP(strm->bit_per_sample);
405 state->mode = state->pushed_mode;
414 return AE_STREAM_ERROR;