4 * @author Mathis Rosenhauer, Deutsches Klimarechenzentrum
5 * @author Moritz Hanke, Deutsches Klimarechenzentrum
6 * @author Joerg Behrens, Deutsches Klimarechenzentrum
7 * @author Luis Kornblueh, Max-Planck-Institut fuer Meteorologie
12 * Mathis Rosenhauer, Luis Kornblueh
16 * Deutsches Klimarechenzentrum GmbH Max-Planck-Institut fuer Meteorologie
17 * Bundesstr. 45a Bundesstr. 53
18 * 20146 Hamburg 20146 Hamburg
21 * All rights reserved.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
27 * 1. Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above
30 * copyright notice, this list of conditions and the following
31 * disclaimer in the documentation and/or other materials provided
32 * with the distribution.
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
37 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
38 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
39 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
45 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * @section DESCRIPTION
49 * Adaptive Entropy Decoder
50 * Based on CCSDS documents 121.0-B-2 and 120.0-G-2
70 #define BUFFERSPACE(strm) (strm->avail_in >= strm->state->in_blklen \
71 && strm->avail_out >= strm->state->out_blklen)
74 static void flush_##KIND(struct aec_stream *strm) \
76 uint32_t *bp, *flush_end; \
78 int64_t data, med, half_d, xmin, xmax; \
79 struct internal_state *state = strm->state; \
81 flush_end = state->rsip; \
83 if (state->flush_start == state->rsi_buffer \
84 && state->rsip > state->rsi_buffer) { \
85 state->last_out = *state->rsi_buffer; \
87 if (strm->flags & AEC_DATA_SIGNED) { \
88 m = 1ULL << (strm->bits_per_sample - 1); \
89 /* Reference samples have to be sign extended */ \
90 state->last_out = (state->last_out ^ m) - m; \
92 put_##KIND(strm, state->last_out); \
93 state->flush_start++; \
96 data = state->last_out; \
97 if (strm->flags & AEC_DATA_SIGNED) \
100 med = (state->xmax - state->xmin) / 2 + 1; \
102 xmin = state->xmin; \
103 xmax = state->xmax; \
105 for (bp = state->flush_start; bp < flush_end; bp++) { \
107 half_d = (d + 1) >> 1; \
110 if (half_d <= data - xmin) { \
119 if (half_d <= xmax - data) { \
128 put_##KIND(strm, data); \
130 state->last_out = data; \
132 for (bp = state->flush_start; bp < flush_end; bp++) \
133 put_##KIND(strm, *bp); \
135 state->flush_start = state->rsip; \
139 static inline void put_msb_32(struct aec_stream *strm, uint32_t data)
141 *strm->next_out++ = data >> 24;
142 *strm->next_out++ = data >> 16;
143 *strm->next_out++ = data >> 8;
144 *strm->next_out++ = data;
147 static inline void put_msb_24(struct aec_stream *strm, uint32_t data)
149 *strm->next_out++ = data >> 16;
150 *strm->next_out++ = data >> 8;
151 *strm->next_out++ = data;
154 static inline void put_msb_16(struct aec_stream *strm, uint32_t data)
156 *strm->next_out++ = data >> 8;
157 *strm->next_out++ = data;
160 static inline void put_lsb_32(struct aec_stream *strm, uint32_t data)
162 *strm->next_out++ = data;
163 *strm->next_out++ = data >> 8;
164 *strm->next_out++ = data >> 16;
165 *strm->next_out++ = data >> 24;
168 static inline void put_lsb_24(struct aec_stream *strm, uint32_t data)
170 *strm->next_out++ = data;
171 *strm->next_out++ = data >> 8;
172 *strm->next_out++ = data >> 16;
175 static inline void put_lsb_16(struct aec_stream *strm, uint32_t data)
177 *strm->next_out++ = data;
178 *strm->next_out++ = data >> 8;
181 static inline void put_8(struct aec_stream *strm, uint32_t data)
183 *strm->next_out++ = data;
194 static inline void check_rsi_end(struct aec_stream *strm)
197 Flush output if end of RSI reached
199 struct internal_state *state = strm->state;
201 if (state->rsip - state->rsi_buffer == state->rsi_size) {
202 state->flush_output(strm);
203 state->flush_start = state->rsi_buffer;
204 state->rsip = state->rsi_buffer;
208 static inline void put_sample(struct aec_stream *strm, uint32_t s)
210 struct internal_state *state = strm->state;
213 strm->avail_out -= state->bytes_per_sample;
217 static inline void fill_acc(struct aec_stream *strm)
219 int b = (63 - strm->state->bitp) >> 3;
222 strm->state->bitp += b << 3;
226 strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
228 strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
230 strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
232 strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
234 strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
236 strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
238 strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
243 static inline uint32_t direct_get(struct aec_stream *strm, unsigned int n)
246 Get n bit from input stream
248 No checking whatsoever. Read bits are dumped.
251 struct internal_state *state = strm->state;
257 return (state->acc >> state->bitp) & ((1ULL << n) - 1);
260 static inline uint32_t direct_get_fs(struct aec_stream *strm)
263 Interpret a Fundamental Sequence from the input buffer.
265 Essentially counts the number of 0 bits until a 1 is
266 encountered. The longest FS we can safely detect is 56 bits. If
267 no FS is found in accumulator then we top it up to at least 56
272 struct internal_state *state = strm->state;
274 state->acc &= ((1ULL << state->bitp) - 1);
279 #ifdef HAVE_DECL___BUILTIN_CLZLL
280 fs = __builtin_clzll(state->acc) - (64 - state->bitp);
281 state->bitp -= fs + 1;
284 while ((state->acc & (1ULL << state->bitp)) == 0) {
292 static inline uint32_t bits_ask(struct aec_stream *strm, int n)
294 while (strm->state->bitp < n) {
295 if (strm->avail_in == 0)
298 strm->state->acc <<= 8;
299 strm->state->acc |= *strm->next_in++;
300 strm->state->bitp += 8;
305 static inline uint32_t bits_get(struct aec_stream *strm, int n)
307 return (strm->state->acc >> (strm->state->bitp - n))
311 static inline void bits_drop(struct aec_stream *strm, int n)
313 strm->state->bitp -= n;
316 static inline uint32_t fs_ask(struct aec_stream *strm)
318 if (bits_ask(strm, 1) == 0)
320 while ((strm->state->acc & (1ULL << (strm->state->bitp - 1))) == 0) {
321 if (strm->state->bitp == 1) {
322 if (strm->avail_in == 0)
325 strm->state->acc <<= 8;
326 strm->state->acc |= *strm->next_in++;
327 strm->state->bitp += 8;
335 static inline void fs_drop(struct aec_stream *strm)
341 static inline uint32_t copysample(struct aec_stream *strm)
343 if (bits_ask(strm, strm->bits_per_sample) == 0
344 || strm->avail_out == 0)
347 put_sample(strm, bits_get(strm, strm->bits_per_sample));
348 bits_drop(strm, strm->bits_per_sample);
352 static int m_id(struct aec_stream *strm)
354 struct internal_state *state = strm->state;
356 if (state->pp && state->rsip == state->rsi_buffer)
361 if (bits_ask(strm, state->id_len) == 0)
363 state->id = bits_get(strm, state->id_len);
364 bits_drop(strm, state->id_len);
365 state->mode = state->id_table[state->id];
370 static int m_split_output(struct aec_stream *strm)
372 struct internal_state *state = strm->state;
373 int k = state->id - 1;
376 if (bits_ask(strm, k) == 0 || strm->avail_out == 0)
378 *state->rsip++ += bits_get(strm, k);
379 strm->avail_out -= state->bytes_per_sample;
381 } while(++state->i < state->n);
388 static int m_split_fs(struct aec_stream *strm)
390 struct internal_state *state = strm->state;
391 int k = state->id - 1;
394 if (fs_ask(strm) == 0)
396 state->rsip[state->i] = state->fs << k;
398 } while(++state->i < state->n);
401 state->mode = m_split_output;
405 static int m_split(struct aec_stream *strm)
408 struct internal_state *state = strm->state;
410 if (BUFFERSPACE(strm)) {
414 *state->rsip++ = direct_get(strm, strm->bits_per_sample);
416 for (i = 0; i < strm->block_size - state->ref; i++)
417 state->rsip[i] = direct_get_fs(strm) << k;
419 for (i = state->ref; i < strm->block_size; i++)
420 *state->rsip++ += direct_get(strm, k);
422 strm->avail_out -= state->out_blklen;
430 if (copysample(strm) == 0)
432 state->n = strm->block_size - 1;
434 state->n = strm->block_size;
438 state->mode = m_split_fs;
442 static int m_zero_output(struct aec_stream *strm)
444 struct internal_state *state = strm->state;
447 if (strm->avail_out == 0)
456 static int m_zero_block(struct aec_stream *strm)
458 int i, zero_blocks, b, zero_bytes;
459 struct internal_state *state = strm->state;
461 if (fs_ask(strm) == 0)
463 zero_blocks = state->fs + 1;
466 if (zero_blocks == ROS) {
467 b = (state->rsip - state->rsi_buffer) / strm->block_size;
468 zero_blocks = MIN(strm->rsi - b, 64 - (b % 64));
469 } else if (zero_blocks > ROS) {
474 i = zero_blocks * strm->block_size - 1;
476 i = zero_blocks * strm->block_size;
478 zero_bytes = i * state->bytes_per_sample;
479 if (strm->avail_out >= zero_bytes) {
480 memset(state->rsip, 0, i * sizeof(uint32_t));
482 strm->avail_out -= zero_bytes;
490 state->mode = m_zero_output;
494 static int m_se_decode(struct aec_stream *strm)
497 struct internal_state *state = strm->state;
499 while(state->i < strm->block_size) {
500 if (fs_ask(strm) == 0)
503 d1 = m - state->se_table[2 * m + 1];
505 if ((state->i & 1) == 0) {
506 if (strm->avail_out == 0)
508 put_sample(strm, state->se_table[2 * m] - d1);
512 if (strm->avail_out == 0)
514 put_sample(strm, d1);
523 static int m_se(struct aec_stream *strm)
527 struct internal_state *state = strm->state;
529 if (BUFFERSPACE(strm)) {
532 while (i < strm->block_size) {
533 m = direct_get_fs(strm);
534 d1 = m - state->se_table[2 * m + 1];
537 put_sample(strm, state->se_table[2 * m] - d1);
540 put_sample(strm, d1);
547 state->mode = m_se_decode;
548 state->i = state->ref;
552 static int m_low_entropy_ref(struct aec_stream *strm)
554 struct internal_state *state = strm->state;
556 if (state->ref && copysample(strm) == 0)
564 state->mode = m_zero_block;
568 static int m_low_entropy(struct aec_stream *strm)
570 struct internal_state *state = strm->state;
572 if (bits_ask(strm, 1) == 0)
574 state->id = bits_get(strm, 1);
576 state->mode = m_low_entropy_ref;
580 static int m_uncomp_copy(struct aec_stream *strm)
582 struct internal_state *state = strm->state;
585 if (copysample(strm) == 0)
593 static int m_uncomp(struct aec_stream *strm)
596 struct internal_state *state = strm->state;
598 if (BUFFERSPACE(strm)) {
599 for (i = 0; i < strm->block_size; i++)
600 *state->rsip++ = direct_get(strm, strm->bits_per_sample);
601 strm->avail_out -= state->out_blklen;
608 state->i = strm->block_size;
609 state->mode = m_uncomp_copy;
613 static void create_se_table(int *table)
618 for (i = 0; i < 13; i++) {
620 for (j = 0; j <= i; j++) {
622 table[2 * k + 1] = ms;
628 int aec_decode_init(struct aec_stream *strm)
631 struct internal_state *state;
633 if (strm->bits_per_sample > 32 || strm->bits_per_sample == 0)
634 return AEC_CONF_ERROR;
636 state = malloc(sizeof(struct internal_state));
638 return AEC_MEM_ERROR;
640 create_se_table(state->se_table);
644 if (strm->bits_per_sample > 16) {
647 if (strm->bits_per_sample <= 24 && strm->flags & AEC_DATA_3BYTE) {
648 state->bytes_per_sample = 3;
649 if (strm->flags & AEC_DATA_MSB)
650 state->flush_output = flush_msb_24;
652 state->flush_output = flush_lsb_24;
654 state->bytes_per_sample = 4;
655 if (strm->flags & AEC_DATA_MSB)
656 state->flush_output = flush_msb_32;
658 state->flush_output = flush_lsb_32;
660 state->out_blklen = strm->block_size
661 * state->bytes_per_sample;
663 else if (strm->bits_per_sample > 8) {
664 state->bytes_per_sample = 2;
666 state->out_blklen = strm->block_size * 2;
667 if (strm->flags & AEC_DATA_MSB)
668 state->flush_output = flush_msb_16;
670 state->flush_output = flush_lsb_16;
672 if (strm->flags & AEC_DATA_RESTRICT) {
673 if (strm->bits_per_sample <= 4) {
674 if (strm->bits_per_sample <= 2)
679 return AEC_CONF_ERROR;
685 state->bytes_per_sample = 1;
686 state->out_blklen = strm->block_size;
687 state->flush_output = flush_8;
690 if (strm->flags & AEC_DATA_SIGNED) {
691 state->xmin = -(1ULL << (strm->bits_per_sample - 1));
692 state->xmax = (1ULL << (strm->bits_per_sample - 1)) - 1;
695 state->xmax = (1ULL << strm->bits_per_sample) - 1;
698 state->in_blklen = (strm->block_size * strm->bits_per_sample
699 + state->id_len) / 8 + 9;
701 modi = 1UL << state->id_len;
702 state->id_table = malloc(modi * sizeof(int (*)(struct aec_stream *)));
703 if (state->id_table == NULL)
704 return AEC_MEM_ERROR;
706 state->id_table[0] = m_low_entropy;
707 for (i = 1; i < modi - 1; i++) {
708 state->id_table[i] = m_split;
710 state->id_table[modi - 1] = m_uncomp;
712 state->rsi_size = strm->rsi * strm->block_size;
713 state->rsi_buffer = malloc(state->rsi_size * sizeof(uint32_t));
714 if (state->rsi_buffer == NULL)
715 return AEC_MEM_ERROR;
720 state->rsip = state->rsi_buffer;
721 state->flush_start = state->rsi_buffer;
724 state->pp = strm->flags & AEC_DATA_PREPROCESS;
729 int aec_decode(struct aec_stream *strm, int flush)
732 Finite-state machine implementation of the adaptive entropy
735 Can work with one byte input und one sample output buffers. If
736 enough buffer space is available, then faster implementations
737 of the states are called. Inspired by zlib.
740 struct internal_state *state = strm->state;
742 strm->total_in += strm->avail_in;
743 strm->total_out += strm->avail_out;
745 while (state->mode(strm) == M_CONTINUE);
746 state->flush_output(strm);
748 strm->total_in -= strm->avail_in;
749 strm->total_out -= strm->avail_out;
754 int aec_decode_end(struct aec_stream *strm)
756 struct internal_state *state = strm->state;
758 free(state->id_table);
759 free(state->rsi_buffer);
764 int aec_buffer_decode(struct aec_stream *strm)
768 status = aec_decode_init(strm);
769 if (status != AEC_OK)
772 status = aec_decode(strm, AEC_FLUSH);
773 aec_decode_end(strm);