3 * @author Mathis Rosenhauer, Deutsches Klimarechenzentrum
6 * Adaptive Entropy Encoder
7 * Based on CCSDS documents 121.0-B-2 and 120.0-G-2
24 #include "encode_accessors.h"
26 /* Marker for Remainder Of Segment condition in zero block encoding */
29 #define MIN(a, b) (((a) < (b))? (a): (b))
31 static int m_get_block(struct aec_stream *strm);
32 static int m_get_block_cautious(struct aec_stream *strm);
33 static int m_check_zero_block(struct aec_stream *strm);
34 static int m_select_code_option(struct aec_stream *strm);
35 static int m_flush_block(struct aec_stream *strm);
36 static int m_flush_block_cautious(struct aec_stream *strm);
37 static int m_encode_splitting(struct aec_stream *strm);
38 static int m_encode_uncomp(struct aec_stream *strm);
39 static int m_encode_se(struct aec_stream *strm);
40 static int m_encode_zero(struct aec_stream *strm);
42 static inline void emit(struct internal_state *state,
43 uint32_t data, int bits)
46 Emit sequence of bits.
49 if (bits <= state->bit_p) {
51 *state->cds_p += data << state->bit_p;
54 *state->cds_p++ += (uint64_t)data >> bits;
58 *state->cds_p++ = data >> bits;
61 state->bit_p = 8 - bits;
62 *state->cds_p = data << state->bit_p;
66 static inline void emitfs(struct internal_state *state, int fs)
69 Emits a fundamental sequence.
71 fs zero bits followed by one 1 bit.
75 if (fs < state->bit_p) {
76 state->bit_p -= fs + 1;
77 *state->cds_p += 1 << state->bit_p;
87 #define EMITBLOCK(ref) \
88 static inline void emitblock_##ref(struct aec_stream *strm, \
93 struct internal_state *state = strm->state; \
94 uint32_t *in = state->block_p + ref; \
95 uint32_t *in_end = state->block_p + strm->block_size; \
96 uint64_t mask = (1ULL << k) - 1; \
97 uint8_t *o = state->cds_p; \
98 int p = state->bit_p; \
102 while(in < in_end) { \
106 while (p > k && in < in_end) { \
108 a += ((uint64_t)(*in++) & mask) << p; \
111 for (b = 56; b > (p & ~7); b -= 8) \
118 state->bit_p = p % 8; \
124 static void preprocess_unsigned(struct aec_stream *strm)
127 int64_t theta, Delta, prev;
128 struct internal_state *state = strm->state;
130 prev = state->block_buf[0];
132 for (i = 1; i < strm->rsi * strm->block_size; i++) {
133 theta = MIN(prev, state->xmax - prev);
134 Delta = (int64_t)state->block_buf[i] - prev;
135 prev = state->block_buf[i];
137 if (0 <= Delta && Delta <= theta) {
138 state->block_buf[i] = 2 * Delta;
139 } else if (-theta <= Delta && Delta < 0) {
140 state->block_buf[i] = 2
141 * (Delta < 0 ? -(uint64_t)Delta : Delta) - 1;
143 state->block_buf[i] = theta
144 + (Delta < 0 ? -(uint64_t)Delta : Delta);
149 static void preprocess_signed(struct aec_stream *strm)
152 int64_t theta, Delta, prev, sample;
153 struct internal_state *state = strm->state;
155 m = 64 - strm->bit_per_sample;
156 prev = ((int64_t)state->block_buf[0] << m) >> m;
158 for (i = 1; i < strm->rsi * strm->block_size; i++) {
159 theta = MIN(prev - state->xmin, state->xmax - prev);
160 sample = ((int64_t)state->block_buf[i] << m) >> m;
161 Delta = sample - prev;
164 if (0 <= Delta && Delta <= theta) {
165 state->block_buf[i] = 2 * Delta;
166 } else if (-theta <= Delta && Delta < 0) {
167 state->block_buf[i] = 2
168 * (Delta < 0 ? -(uint64_t)Delta : Delta) - 1;
170 state->block_buf[i] = theta
171 + (Delta < 0 ? -(uint64_t)Delta : Delta);
182 static int m_get_block(struct aec_stream *strm)
184 struct internal_state *state = strm->state;
186 if (strm->avail_out > state->cds_len) {
187 if (!state->direct_out) {
188 state->direct_out = 1;
189 *strm->next_out = *state->cds_p;
190 state->cds_p = strm->next_out;
193 if (state->zero_blocks == 0 || state->direct_out) {
194 /* copy leftover from last block */
195 *state->cds_buf = *state->cds_p;
196 state->cds_p = state->cds_buf;
198 state->direct_out = 0;
201 if (state->blocks_avail == 0) {
203 state->block_p = state->block_buf;
205 if (strm->avail_in >= state->block_len * strm->rsi) {
206 state->get_block(strm);
207 state->blocks_avail = strm->rsi - 1;
209 if (strm->flags & AEC_DATA_PREPROCESS)
210 state->preprocess(strm);
212 return m_check_zero_block(strm);
215 state->mode = m_get_block_cautious;
219 state->block_p += strm->block_size;
220 state->blocks_avail--;
221 return m_check_zero_block(strm);
226 static int input_empty(struct aec_stream *strm)
229 struct internal_state *state = strm->state;
231 if (state->flush == AEC_FLUSH) {
233 for (j = state->i; j < strm->rsi * strm->block_size; j++)
234 state->block_buf[j] = state->block_buf[state->i - 1];
235 state->i = strm->rsi * strm->block_size;
237 if (state->zero_blocks) {
238 state->mode = m_encode_zero;
242 emit(state, 0, state->bit_p);
243 if (state->direct_out == 0)
244 *strm->next_out++ = *state->cds_p;
255 static int m_get_block_cautious(struct aec_stream *strm)
257 struct internal_state *state = strm->state;
260 if (strm->avail_in > 0)
261 state->block_buf[state->i] = state->get_sample(strm);
263 return input_empty(strm);
264 } while (++state->i < strm->rsi * strm->block_size);
266 state->blocks_avail = strm->rsi - 1;
267 if (strm->flags & AEC_DATA_PREPROCESS)
268 state->preprocess(strm);
270 return m_check_zero_block(strm);
273 static int m_check_zero_block(struct aec_stream *strm)
276 struct internal_state *state = strm->state;
279 while(i < strm->block_size && state->block_p[i] == 0)
282 if (i == strm->block_size) {
283 if (state->zero_blocks == 0) {
284 state->zero_ref = state->ref;
285 state->zero_ref_sample = state->block_p[0];
288 state->zero_blocks++;
290 if ((strm->rsi - state->blocks_avail) % 64 == 0) {
291 if (state->zero_blocks > 4)
292 state->zero_blocks = ROS;
293 state->mode = m_encode_zero;
296 state->mode = m_get_block;
298 } else if (state->zero_blocks) {
299 /* The current block isn't zero but we have to emit a previous
300 * zero block first. The current block will be handled
303 state->block_p -= strm->block_size;
304 state->blocks_avail++;
305 state->mode = m_encode_zero;
308 state->mode = m_select_code_option;
312 static uint64_t block_fs(struct aec_stream *strm, int k)
316 struct internal_state *state = strm->state;
318 fs = (uint64_t)(state->block_p[1] >> k)
319 + (uint64_t)(state->block_p[2] >> k)
320 + (uint64_t)(state->block_p[3] >> k)
321 + (uint64_t)(state->block_p[4] >> k)
322 + (uint64_t)(state->block_p[5] >> k)
323 + (uint64_t)(state->block_p[6] >> k)
324 + (uint64_t)(state->block_p[7] >> k);
326 if (strm->block_size > 8)
327 for (j = 1; j < strm->block_size / 8; j++)
329 (uint64_t)(state->block_p[j * 8 + 0] >> k)
330 + (uint64_t)(state->block_p[j * 8 + 1] >> k)
331 + (uint64_t)(state->block_p[j * 8 + 2] >> k)
332 + (uint64_t)(state->block_p[j * 8 + 3] >> k)
333 + (uint64_t)(state->block_p[j * 8 + 4] >> k)
334 + (uint64_t)(state->block_p[j * 8 + 5] >> k)
335 + (uint64_t)(state->block_p[j * 8 + 6] >> k)
336 + (uint64_t)(state->block_p[j * 8 + 7] >> k);
339 fs += (uint64_t)(state->block_p[0] >> k);
344 static int count_splitting_option(struct aec_stream *strm)
347 Find the best point for splitting samples in a block.
349 In Rice coding each sample in a block of samples is split at
350 the same position into k LSB and bit_per_sample - k MSB. The
351 LSB part is left binary and the MSB part is coded as a
352 fundamental sequence a.k.a. unary (see CCSDS 121.0-B-2). The
353 function of the length of the Coded Data Set (CDS) depending on
354 k has exactly one minimum (see A. Kiely, IPN Progress Report
357 To find that minimum with only a few costly evaluations of the
358 CDS length, we start with the k of the previous CDS. K is
359 increased and the CDS length evaluated. If the CDS length gets
360 smaller, then we are moving towards the minimum. If the length
361 increases, then the minimum will be found with smaller k.
363 For increasing k we know that we will gain block_size bits in
364 length through the larger binary part. If the FS lenth is less
365 than the block size then a reduced FS part can't compensate the
366 larger binary part. So we know that the CDS for k+1 will be
367 larger than for k without actually computing the length. An
368 analogue check can be done for decreasing k.
373 int this_bs; /* Block size of current block */
374 int no_turn; /* 1 if we shouldn't reverse */
375 int dir; /* Direction, 1 means increasing k, 0 decreasing k */
376 uint64_t len; /* CDS length for current k */
377 uint64_t len_min; /* CDS length minimum so far */
378 uint64_t fs_len; /* Length of FS part (not including 1s) */
380 struct internal_state *state = strm->state;
382 this_bs = strm->block_size - state->ref;
383 len_min = UINT64_MAX;
384 k = k_min = state->k;
385 no_turn = (k == 0) ? 1 : 0;
389 fs_len = block_fs(strm, k);
390 len = fs_len + this_bs * (k + 1);
393 if (len_min < UINT64_MAX)
400 if (fs_len < this_bs || k >= state->kmax) {
409 if (fs_len >= this_bs || k == 0)
426 static int count_se_option(uint64_t limit, struct aec_stream *strm)
430 struct internal_state *state = strm->state;
434 for (i = 0; i < strm->block_size; i+= 2) {
435 d = (uint64_t)state->block_p[i]
436 + (uint64_t)state->block_p[i + 1];
437 /* we have to worry about overflow here */
442 len += d * (d + 1) / 2
443 + (uint64_t)state->block_p[i + 1];
449 static int m_select_code_option(struct aec_stream *strm)
451 uint64_t uncomp_len, split_len, se_len;
452 struct internal_state *state = strm->state;
454 uncomp_len = (strm->block_size - state->ref)
455 * strm->bit_per_sample;
456 split_len = count_splitting_option(strm);
457 se_len = count_se_option(split_len, strm);
459 if (split_len < uncomp_len) {
460 if (split_len < se_len)
461 return m_encode_splitting(strm);
463 return m_encode_se(strm);
465 if (uncomp_len <= se_len)
466 return m_encode_uncomp(strm);
468 return m_encode_se(strm);
472 static int m_encode_splitting(struct aec_stream *strm)
475 struct internal_state *state = strm->state;
478 emit(state, k + 1, state->id_len);
482 emit(state, state->block_p[0], strm->bit_per_sample);
483 for (i = 1; i < strm->block_size; i++)
484 emitfs(state, state->block_p[i] >> k);
485 if (k) emitblock_1(strm, k);
489 for (i = 0; i < strm->block_size; i++)
490 emitfs(state, state->block_p[i] >> k);
491 if (k) emitblock_0(strm, k);
494 return m_flush_block(strm);
497 static int m_encode_uncomp(struct aec_stream *strm)
499 struct internal_state *state = strm->state;
501 emit(state, (1 << state->id_len) - 1, state->id_len);
502 emitblock_0(strm, strm->bit_per_sample);
504 return m_flush_block(strm);
507 static int m_encode_se(struct aec_stream *strm)
511 struct internal_state *state = strm->state;
513 emit(state, 1, state->id_len + 1);
515 emit(state, state->block_p[0], strm->bit_per_sample);
517 for (i = 0; i < strm->block_size; i+= 2) {
518 d = state->block_p[i] + state->block_p[i + 1];
519 emitfs(state, d * (d + 1) / 2 + state->block_p[i + 1]);
522 return m_flush_block(strm);
525 static int m_encode_zero(struct aec_stream *strm)
527 struct internal_state *state = strm->state;
529 emit(state, 0, state->id_len + 1);
532 emit(state, state->zero_ref_sample, strm->bit_per_sample);
534 if (state->zero_blocks == ROS)
536 else if (state->zero_blocks >= 5)
537 emitfs(state, state->zero_blocks);
539 emitfs(state, state->zero_blocks - 1);
541 state->zero_blocks = 0;
542 return m_flush_block(strm);
545 static int m_flush_block(struct aec_stream *strm)
548 Flush block in direct_out mode by updating counters.
550 Fall back to slow flushing if in buffered mode.
553 struct internal_state *state = strm->state;
555 if (state->direct_out) {
556 n = state->cds_p - strm->next_out;
558 strm->avail_out -= n;
559 strm->total_out += n;
560 state->mode = m_get_block;
565 state->mode = m_flush_block_cautious;
569 static int m_flush_block_cautious(struct aec_stream *strm)
572 Slow and restartable flushing
574 struct internal_state *state = strm->state;
576 while(state->cds_buf + state->i < state->cds_p) {
577 if (strm->avail_out == 0)
580 *strm->next_out++ = state->cds_buf[state->i];
585 state->mode = m_get_block;
595 int aec_encode_init(struct aec_stream *strm)
598 struct internal_state *state;
600 if (strm->bit_per_sample > 32 || strm->bit_per_sample == 0)
601 return AEC_CONF_ERROR;
603 if (strm->block_size != 8
604 && strm->block_size != 16
605 && strm->block_size != 32
606 && strm->block_size != 64)
607 return AEC_CONF_ERROR;
609 if (strm->rsi > 4096)
610 return AEC_CONF_ERROR;
612 state = (struct internal_state *)malloc(sizeof(struct internal_state));
614 return AEC_MEM_ERROR;
616 memset(state, 0, sizeof(struct internal_state));
619 bs = strm->block_size >> 3;
624 if (strm->bit_per_sample > 16) {
625 /* 24/32 input bit settings */
628 if (strm->bit_per_sample <= 24
629 && strm->flags & AEC_DATA_3BYTE) {
630 state->block_len = 3 * strm->block_size;
631 if (strm->flags & AEC_DATA_MSB) {
632 state->get_sample = get_msb_24;
633 state->get_block = get_block_funcs_msb_24[bsi];
635 state->get_sample = get_lsb_24;
636 state->get_block = get_block_funcs_lsb_24[bsi];
639 state->block_len = 4 * strm->block_size;
640 if (strm->flags & AEC_DATA_MSB) {
641 state->get_sample = get_msb_32;
642 state->get_block = get_block_funcs_msb_32[bsi];
644 state->get_sample = get_lsb_32;
645 state->get_block = get_block_funcs_lsb_32[bsi];
649 else if (strm->bit_per_sample > 8) {
650 /* 16 bit settings */
652 state->block_len = 2 * strm->block_size;
654 if (strm->flags & AEC_DATA_MSB) {
655 state->get_sample = get_msb_16;
656 state->get_block = get_block_funcs_msb_16[bsi];
658 state->get_sample = get_lsb_16;
659 state->get_block = get_block_funcs_lsb_16[bsi];
664 state->block_len = strm->block_size;
666 state->get_sample = get_8;
667 state->get_block = get_block_funcs_8[bsi];
670 if (strm->flags & AEC_DATA_SIGNED) {
671 state->xmin = -(1ULL << (strm->bit_per_sample - 1));
672 state->xmax = (1ULL << (strm->bit_per_sample - 1)) - 1;
673 state->preprocess = preprocess_signed;
676 state->xmax = (1ULL << strm->bit_per_sample) - 1;
677 state->preprocess = preprocess_unsigned;
680 state->kmax = (1U << state->id_len) - 3;
682 state->block_buf = (uint32_t *)malloc(strm->rsi
685 if (state->block_buf == NULL)
686 return AEC_MEM_ERROR;
688 state->block_p = state->block_buf;
690 /* Largest possible CDS according to specs */
691 state->cds_len = (5 + 64 * 32) / 8 + 3;
692 state->cds_buf = (uint8_t *)malloc(state->cds_len);
693 if (state->cds_buf == NULL)
694 return AEC_MEM_ERROR;
699 state->cds_p = state->cds_buf;
702 state->mode = m_get_block;
707 int aec_encode(struct aec_stream *strm, int flush)
710 Finite-state machine implementation of the adaptive entropy
714 struct internal_state *state;
716 state->flush = flush;
718 while (state->mode(strm) == M_CONTINUE);
720 if (state->direct_out) {
721 n = state->cds_p - strm->next_out;
723 strm->avail_out -= n;
724 strm->total_out += n;
726 *state->cds_buf = *state->cds_p;
727 state->cds_p = state->cds_buf;
728 state->direct_out = 0;
733 int aec_encode_end(struct aec_stream *strm)
735 struct internal_state *state = strm->state;
737 free(state->block_buf);
738 free(state->cds_buf);