5 * Copyright 2012 - 2014
7 * Mathis Rosenhauer, Moritz Hanke, Joerg Behrens
8 * Deutsches Klimarechenzentrum GmbH
10 * 20146 Hamburg Germany
13 * Max-Planck-Institut fuer Meteorologie
18 * All rights reserved.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above
27 * copyright notice, this list of conditions and the following
28 * disclaimer in the documentation and/or other materials provided
29 * with the distribution.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
34 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
35 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
36 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
38 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
42 * OF THE POSSIBILITY OF SUCH DAMAGE.
44 * @section DESCRIPTION
46 * Adaptive Entropy Decoder
47 * Based on CCSDS documents 121.0-B-2 and 120.0-G-3
64 #define BUFFERSPACE(strm) (strm->avail_in >= strm->state->in_blklen \
65 && strm->avail_out >= strm->state->out_blklen)
68 static void flush_##KIND(struct aec_stream *strm) \
70 uint32_t *flush_end, *bp, half_d; \
72 struct internal_state *state = strm->state; \
74 flush_end = state->rsip; \
76 if (state->flush_start == state->rsi_buffer \
77 && state->rsip > state->rsi_buffer) { \
78 state->last_out = *state->rsi_buffer; \
80 if (strm->flags & AEC_DATA_SIGNED) { \
81 m = UINT32_C(1) << (strm->bits_per_sample - 1); \
82 /* Reference samples have to be sign extended */ \
83 state->last_out = (state->last_out ^ m) - m; \
85 put_##KIND(strm, (uint32_t)state->last_out); \
86 state->flush_start++; \
89 data = state->last_out; \
91 if (state->xmin == 0) { \
93 uint32_t xmax, med, d; \
94 med = state->xmax / 2 + 1; \
97 for (bp = state->flush_start; bp < flush_end; bp++) { \
99 half_d = (d >> 1) + (d & 1); \
100 /*in this case: data >= med == data & med */ \
101 uint32_t mask = (data & med)?xmax:0; \
103 /*in this case: xmax - data == xmax ^ data */ \
104 if (half_d <= (mask ^ data)) { \
105 data += (d >> 1)^(~((d & 1) - 1)); \
109 put_##KIND(strm, (uint32_t)data); \
111 state->last_out = data; \
116 xmax = state->xmax; \
118 for (bp = state->flush_start; bp < flush_end; bp++) { \
120 half_d = ((uint32_t)d >> 1) + (d & 1); \
123 if (half_d <= xmax + data + 1) { \
124 data += ((uint32_t)d >> 1)^(~((d & 1) - 1)); \
126 data = d - xmax - 1; \
129 if (half_d <= xmax - data) { \
130 data += ((uint32_t)d >> 1)^(~((d & 1) - 1)); \
135 put_##KIND(strm, (uint32_t)data); \
137 state->last_out = data; \
140 for (bp = state->flush_start; bp < flush_end; bp++) \
141 put_##KIND(strm, *bp); \
143 state->flush_start = state->rsip; \
147 static inline void put_msb_32(struct aec_stream *strm, uint32_t data)
149 *strm->next_out++ = (unsigned char)(data >> 24);
150 *strm->next_out++ = (unsigned char)(data >> 16);
151 *strm->next_out++ = (unsigned char)(data >> 8);
152 *strm->next_out++ = (unsigned char)data;
155 static inline void put_msb_24(struct aec_stream *strm, uint32_t data)
157 *strm->next_out++ = (unsigned char)(data >> 16);
158 *strm->next_out++ = (unsigned char)(data >> 8);
159 *strm->next_out++ = (unsigned char)data;
162 static inline void put_msb_16(struct aec_stream *strm, uint32_t data)
164 *strm->next_out++ = (unsigned char)(data >> 8);
165 *strm->next_out++ = (unsigned char)data;
168 static inline void put_lsb_32(struct aec_stream *strm, uint32_t data)
170 *strm->next_out++ = (unsigned char)data;
171 *strm->next_out++ = (unsigned char)(data >> 8);
172 *strm->next_out++ = (unsigned char)(data >> 16);
173 *strm->next_out++ = (unsigned char)(data >> 24);
176 static inline void put_lsb_24(struct aec_stream *strm, uint32_t data)
178 *strm->next_out++ = (unsigned char)data;
179 *strm->next_out++ = (unsigned char)(data >> 8);
180 *strm->next_out++ = (unsigned char)(data >> 16);
183 static inline void put_lsb_16(struct aec_stream *strm, uint32_t data)
185 *strm->next_out++ = (unsigned char)data;
186 *strm->next_out++ = (unsigned char)(data >> 8);
189 static inline void put_8(struct aec_stream *strm, uint32_t data)
191 *strm->next_out++ = (unsigned char)data;
202 static inline void check_rsi_end(struct aec_stream *strm)
205 Flush output if end of RSI reached
207 struct internal_state *state = strm->state;
209 if (state->rsi_size == (size_t)(state->rsip - state->rsi_buffer)) {
210 state->flush_output(strm);
211 state->flush_start = state->rsi_buffer;
212 state->rsip = state->rsi_buffer;
216 static inline void put_sample(struct aec_stream *strm, uint32_t s)
218 struct internal_state *state = strm->state;
221 strm->avail_out -= state->bytes_per_sample;
225 static inline void fill_acc(struct aec_stream *strm)
227 int b = (63 - strm->state->bitp) >> 3;
230 strm->state->bitp += b << 3;
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++;
240 strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
242 strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
244 strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
246 strm->state->acc = (strm->state->acc << 8) | *strm->next_in++;
251 static inline uint32_t direct_get(struct aec_stream *strm, int n)
254 Get n bit from input stream
256 No checking whatsoever. Read bits are dumped.
259 struct internal_state *state = strm->state;
265 return (state->acc >> state->bitp) & ((UINT64_C(1) << n) - 1);
268 static inline uint32_t direct_get_fs(struct aec_stream *strm)
271 Interpret a Fundamental Sequence from the input buffer.
273 Essentially counts the number of 0 bits until a 1 is
278 #if HAVE_DECL___BUILTIN_CLZLL
283 struct internal_state *state = strm->state;
285 state->acc &= ((UINT64_C(1) << state->bitp) - 1);
287 while (state->acc == 0) {
293 #if HAVE_DECL___BUILTIN_CLZLL
294 lz = __builtin_clzll(state->acc);
295 fs += lz + state->bitp - 64;
296 state->bitp = 63 - lz;
298 _BitScanReverse64(&lz, state->acc);
299 fs += state->bitp - 1 - lz;
303 while ((state->acc & (UINT64_C(1) << state->bitp)) == 0) {
311 static inline uint32_t bits_ask(struct aec_stream *strm, int n)
313 while (strm->state->bitp < n) {
314 if (strm->avail_in == 0)
317 strm->state->acc <<= 8;
318 strm->state->acc |= *strm->next_in++;
319 strm->state->bitp += 8;
324 static inline uint32_t bits_get(struct aec_stream *strm, int n)
326 return (strm->state->acc >> (strm->state->bitp - n))
327 & ((UINT64_C(1) << n) - 1);
330 static inline void bits_drop(struct aec_stream *strm, int n)
332 strm->state->bitp -= n;
335 static inline uint32_t fs_ask(struct aec_stream *strm)
337 if (bits_ask(strm, 1) == 0)
339 while ((strm->state->acc & (UINT64_C(1) << (strm->state->bitp - 1))) == 0) {
340 if (strm->state->bitp == 1) {
341 if (strm->avail_in == 0)
344 strm->state->acc <<= 8;
345 strm->state->acc |= *strm->next_in++;
346 strm->state->bitp += 8;
354 static inline void fs_drop(struct aec_stream *strm)
360 static inline uint32_t copysample(struct aec_stream *strm)
362 if (bits_ask(strm, strm->bits_per_sample) == 0
363 || strm->avail_out == 0)
366 put_sample(strm, bits_get(strm, strm->bits_per_sample));
367 bits_drop(strm, strm->bits_per_sample);
371 static int m_id(struct aec_stream *strm)
373 struct internal_state *state = strm->state;
375 if (state->rsip == state->rsi_buffer) {
376 if(strm->flags & AEC_PAD_RSI)
377 state->bitp -= state->bitp % 8;
383 if (bits_ask(strm, state->id_len) == 0)
385 state->id = bits_get(strm, state->id_len);
386 bits_drop(strm, state->id_len);
387 state->mode = state->id_table[state->id];
392 static int m_split_output(struct aec_stream *strm)
394 struct internal_state *state = strm->state;
395 int k = state->id - 1;
398 if (bits_ask(strm, k) == 0 || strm->avail_out == 0)
400 *state->rsip++ += bits_get(strm, k);
401 strm->avail_out -= state->bytes_per_sample;
403 } while(++state->i < state->n);
410 static int m_split_fs(struct aec_stream *strm)
412 struct internal_state *state = strm->state;
413 int k = state->id - 1;
416 if (fs_ask(strm) == 0)
418 state->rsip[state->i] = state->fs << k;
420 } while(++state->i < state->n);
423 state->mode = m_split_output;
427 static int m_split(struct aec_stream *strm)
430 struct internal_state *state = strm->state;
432 if (BUFFERSPACE(strm)) {
436 *state->rsip++ = direct_get(strm, strm->bits_per_sample);
438 for (i = 0; i < strm->block_size - state->ref; i++)
439 state->rsip[i] = direct_get_fs(strm) << k;
441 for (i = state->ref; i < strm->block_size; i++)
442 *state->rsip++ += direct_get(strm, k);
444 strm->avail_out -= state->out_blklen;
452 if (copysample(strm) == 0)
454 state->n = strm->block_size - 1;
456 state->n = strm->block_size;
460 state->mode = m_split_fs;
464 static int m_zero_output(struct aec_stream *strm)
466 struct internal_state *state = strm->state;
469 if (strm->avail_out == 0)
478 static int m_zero_block(struct aec_stream *strm)
480 uint32_t i, zero_blocks, b, zero_bytes;
481 struct internal_state *state = strm->state;
483 if (fs_ask(strm) == 0)
485 zero_blocks = state->fs + 1;
488 if (zero_blocks == ROS) {
489 b = (int)(state->rsip - state->rsi_buffer) / strm->block_size;
490 zero_blocks = MIN(strm->rsi - b, 64 - (b % 64));
491 } else if (zero_blocks > ROS) {
496 i = zero_blocks * strm->block_size - 1;
498 i = zero_blocks * strm->block_size;
500 zero_bytes = i * state->bytes_per_sample;
502 if (strm->avail_out >= zero_bytes) {
503 if (state->rsi_size - (state->rsip - state->rsi_buffer) < i)
506 memset(state->rsip, 0, i * sizeof(uint32_t));
508 strm->avail_out -= zero_bytes;
516 state->mode = m_zero_output;
520 static int m_se_decode(struct aec_stream *strm)
523 struct internal_state *state = strm->state;
525 while(state->i < strm->block_size) {
526 if (fs_ask(strm) == 0)
529 d1 = m - state->se_table[2 * m + 1];
531 if ((state->i & 1) == 0) {
532 if (strm->avail_out == 0)
534 put_sample(strm, state->se_table[2 * m] - d1);
538 if (strm->avail_out == 0)
540 put_sample(strm, d1);
549 static int m_se(struct aec_stream *strm)
553 struct internal_state *state = strm->state;
555 if (BUFFERSPACE(strm)) {
558 while (i < strm->block_size) {
559 m = direct_get_fs(strm);
560 d1 = m - state->se_table[2 * m + 1];
563 put_sample(strm, state->se_table[2 * m] - d1);
566 put_sample(strm, d1);
573 state->mode = m_se_decode;
574 state->i = state->ref;
578 static int m_low_entropy_ref(struct aec_stream *strm)
580 struct internal_state *state = strm->state;
582 if (state->ref && copysample(strm) == 0)
590 state->mode = m_zero_block;
594 static int m_low_entropy(struct aec_stream *strm)
596 struct internal_state *state = strm->state;
598 if (bits_ask(strm, 1) == 0)
600 state->id = bits_get(strm, 1);
602 state->mode = m_low_entropy_ref;
606 static int m_uncomp_copy(struct aec_stream *strm)
608 struct internal_state *state = strm->state;
611 if (copysample(strm) == 0)
619 static int m_uncomp(struct aec_stream *strm)
622 struct internal_state *state = strm->state;
624 if (BUFFERSPACE(strm)) {
625 for (i = 0; i < strm->block_size; i++)
626 *state->rsip++ = direct_get(strm, strm->bits_per_sample);
627 strm->avail_out -= state->out_blklen;
634 state->i = strm->block_size;
635 state->mode = m_uncomp_copy;
639 static void create_se_table(int *table)
644 for (i = 0; i < 13; i++) {
646 for (j = 0; j <= i; j++) {
648 table[2 * k + 1] = ms;
654 int aec_decode_init(struct aec_stream *strm)
657 struct internal_state *state;
659 if (strm->bits_per_sample > 32 || strm->bits_per_sample == 0)
660 return AEC_CONF_ERROR;
662 state = malloc(sizeof(struct internal_state));
664 return AEC_MEM_ERROR;
665 memset(state, 0, sizeof(struct internal_state));
667 create_se_table(state->se_table);
671 if (strm->bits_per_sample > 16) {
674 if (strm->bits_per_sample <= 24 && strm->flags & AEC_DATA_3BYTE) {
675 state->bytes_per_sample = 3;
676 if (strm->flags & AEC_DATA_MSB)
677 state->flush_output = flush_msb_24;
679 state->flush_output = flush_lsb_24;
681 state->bytes_per_sample = 4;
682 if (strm->flags & AEC_DATA_MSB)
683 state->flush_output = flush_msb_32;
685 state->flush_output = flush_lsb_32;
687 state->out_blklen = strm->block_size
688 * state->bytes_per_sample;
690 else if (strm->bits_per_sample > 8) {
691 state->bytes_per_sample = 2;
693 state->out_blklen = strm->block_size * 2;
694 if (strm->flags & AEC_DATA_MSB)
695 state->flush_output = flush_msb_16;
697 state->flush_output = flush_lsb_16;
699 if (strm->flags & AEC_RESTRICTED) {
700 if (strm->bits_per_sample <= 4) {
701 if (strm->bits_per_sample <= 2)
706 return AEC_CONF_ERROR;
712 state->bytes_per_sample = 1;
713 state->out_blklen = strm->block_size;
714 state->flush_output = flush_8;
717 if (strm->flags & AEC_DATA_SIGNED) {
718 state->xmin = -(INT64_C(1) << (strm->bits_per_sample - 1));
719 state->xmax = (UINT64_C(1) << (strm->bits_per_sample - 1)) - 1;
722 state->xmax = (UINT64_C(1) << strm->bits_per_sample) - 1;
725 state->in_blklen = (strm->block_size * strm->bits_per_sample
726 + state->id_len) / 8 + 9;
728 modi = 1UL << state->id_len;
729 state->id_table = malloc(modi * sizeof(int (*)(struct aec_stream *)));
730 if (state->id_table == NULL)
731 return AEC_MEM_ERROR;
733 state->id_table[0] = m_low_entropy;
734 for (i = 1; i < modi - 1; i++) {
735 state->id_table[i] = m_split;
737 state->id_table[modi - 1] = m_uncomp;
739 state->rsi_size = strm->rsi * strm->block_size;
740 state->rsi_buffer = malloc(state->rsi_size * sizeof(uint32_t));
741 if (state->rsi_buffer == NULL)
742 return AEC_MEM_ERROR;
748 state->rsip = state->rsi_buffer;
749 state->flush_start = state->rsi_buffer;
752 state->pp = strm->flags & AEC_DATA_PREPROCESS;
757 int aec_decode(struct aec_stream *strm, int flush)
760 Finite-state machine implementation of the adaptive entropy
763 Can work with one byte input und one sample output buffers. If
764 enough buffer space is available, then faster implementations
765 of the states are called. Inspired by zlib.
768 struct internal_state *state = strm->state;
771 strm->total_in += strm->avail_in;
772 strm->total_out += strm->avail_out;
775 status = state->mode(strm);
776 } while (status == M_CONTINUE);
778 if (status == M_ERROR)
779 return AEC_DATA_ERROR;
781 state->flush_output(strm);
783 strm->total_in -= strm->avail_in;
784 strm->total_out -= strm->avail_out;
789 int aec_decode_end(struct aec_stream *strm)
791 struct internal_state *state = strm->state;
793 free(state->id_table);
794 free(state->rsi_buffer);
799 int aec_buffer_decode(struct aec_stream *strm)
803 status = aec_decode_init(strm);
804 if (status != AEC_OK)
807 status = aec_decode(strm, AEC_FLUSH);
808 aec_decode_end(strm);