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 static int m_get_block(struct aec_stream *strm);
30 static int m_get_block_cautious(struct aec_stream *strm);
31 static int m_check_zero_block(struct aec_stream *strm);
32 static int m_select_code_option(struct aec_stream *strm);
33 static int m_flush_block(struct aec_stream *strm);
34 static int m_flush_block_cautious(struct aec_stream *strm);
35 static int m_encode_splitting(struct aec_stream *strm);
36 static int m_encode_uncomp(struct aec_stream *strm);
37 static int m_encode_se(struct aec_stream *strm);
38 static int m_encode_zero(struct aec_stream *strm);
40 static inline void emit(struct internal_state *state,
41 uint32_t data, int bits)
44 Emit sequence of bits.
47 if (bits <= state->bit_p) {
49 *state->cds_p += data << state->bit_p;
52 *state->cds_p++ += (uint64_t)data >> bits;
56 *state->cds_p++ = data >> bits;
59 state->bit_p = 8 - bits;
60 *state->cds_p = data << state->bit_p;
64 static inline void emitfs(struct internal_state *state, int fs)
67 Emits a fundamental sequence.
69 fs zero bits followed by one 1 bit.
73 if (fs < state->bit_p) {
74 state->bit_p -= fs + 1;
75 *state->cds_p += 1 << state->bit_p;
85 #define EMITBLOCK(ref) \
86 static inline void emitblock_##ref(struct aec_stream *strm, \
91 struct internal_state *state = strm->state; \
92 uint32_t *in = state->block_p + ref; \
93 uint32_t *in_end = state->block_p + strm->block_size; \
94 uint64_t mask = (1ULL << k) - 1; \
95 uint8_t *o = state->cds_p; \
96 int p = state->bit_p; \
100 while(in < in_end) { \
104 while (p > k && in < in_end) { \
106 a += ((uint64_t)(*in++) & mask) << p; \
109 for (b = 56; b > (p & ~7); b -= 8) \
116 state->bit_p = p % 8; \
122 static void preprocess_unsigned(struct aec_stream *strm)
127 struct internal_state *state = strm->state;
128 uint32_t *buf = state->block_buf;
129 int64_t prev = *buf++;
130 uint32_t xmax = state->xmax;
131 uint32_t rsi = strm->rsi * strm->block_size - 1;
152 static void preprocess_signed(struct aec_stream *strm)
158 struct internal_state *state = strm->state;
159 uint32_t *buf = state->block_buf;
160 uint32_t m = 1ULL << (strm->bit_per_sample - 1);
161 int64_t prev = (((int64_t)*buf++) ^ m) - m;
162 int64_t xmax = state->xmax;
163 int64_t xmin = state->xmin;
164 uint32_t rsi = strm->rsi * strm->block_size - 1;
167 v = (((int64_t)*buf) ^ m) - m;
192 static int m_get_block(struct aec_stream *strm)
194 struct internal_state *state = strm->state;
196 if (strm->avail_out > state->cds_len) {
197 if (!state->direct_out) {
198 state->direct_out = 1;
199 *strm->next_out = *state->cds_p;
200 state->cds_p = strm->next_out;
203 if (state->zero_blocks == 0 || state->direct_out) {
204 /* copy leftover from last block */
205 *state->cds_buf = *state->cds_p;
206 state->cds_p = state->cds_buf;
208 state->direct_out = 0;
211 if (state->blocks_avail == 0) {
213 state->block_p = state->block_buf;
215 if (strm->avail_in >= state->block_len * strm->rsi) {
216 state->get_rsi(strm);
217 state->blocks_avail = strm->rsi - 1;
219 if (strm->flags & AEC_DATA_PREPROCESS)
220 state->preprocess(strm);
222 return m_check_zero_block(strm);
225 state->mode = m_get_block_cautious;
229 state->block_p += strm->block_size;
230 state->blocks_avail--;
231 return m_check_zero_block(strm);
236 static int m_get_block_cautious(struct aec_stream *strm)
239 struct internal_state *state = strm->state;
242 if (strm->avail_in > 0) {
243 state->block_buf[state->i] = state->get_sample(strm);
245 if (state->flush == AEC_FLUSH) {
247 for (j = state->i; j < strm->rsi * strm->block_size; j++)
248 state->block_buf[j] = state->block_buf[state->i - 1];
249 state->i = strm->rsi * strm->block_size;
251 if (state->zero_blocks) {
252 state->mode = m_encode_zero;
256 emit(state, 0, state->bit_p);
257 if (state->direct_out == 0)
258 *strm->next_out++ = *state->cds_p;
268 } while (++state->i < strm->rsi * strm->block_size);
270 state->blocks_avail = strm->rsi - 1;
271 if (strm->flags & AEC_DATA_PREPROCESS)
272 state->preprocess(strm);
274 return m_check_zero_block(strm);
277 static int m_check_zero_block(struct aec_stream *strm)
279 struct internal_state *state = strm->state;
280 uint32_t *p = state->block_p + state->ref;
281 uint32_t *end = state->block_p + strm->block_size;
283 while(*p == 0 && p < end)
287 if (state->zero_blocks) {
288 /* The current block isn't zero but we have to emit a
289 * previous zero block first. The current block will be
292 state->block_p -= strm->block_size;
293 state->blocks_avail++;
294 state->mode = m_encode_zero;
297 state->mode = m_select_code_option;
300 state->zero_blocks++;
301 if (state->zero_blocks == 1) {
302 state->zero_ref = state->ref;
303 state->zero_ref_sample = state->block_p[0];
305 if ((strm->rsi - state->blocks_avail) % 64 == 0) {
306 if (state->zero_blocks > 4)
307 state->zero_blocks = ROS;
308 state->mode = m_encode_zero;
311 state->mode = m_get_block;
316 static uint64_t block_fs(struct aec_stream *strm, int k)
320 struct internal_state *state = strm->state;
322 fs = (uint64_t)(state->block_p[1] >> k)
323 + (uint64_t)(state->block_p[2] >> k)
324 + (uint64_t)(state->block_p[3] >> k)
325 + (uint64_t)(state->block_p[4] >> k)
326 + (uint64_t)(state->block_p[5] >> k)
327 + (uint64_t)(state->block_p[6] >> k)
328 + (uint64_t)(state->block_p[7] >> k);
330 if (strm->block_size > 8)
331 for (j = 1; j < strm->block_size / 8; j++)
333 (uint64_t)(state->block_p[j * 8 + 0] >> k)
334 + (uint64_t)(state->block_p[j * 8 + 1] >> k)
335 + (uint64_t)(state->block_p[j * 8 + 2] >> k)
336 + (uint64_t)(state->block_p[j * 8 + 3] >> k)
337 + (uint64_t)(state->block_p[j * 8 + 4] >> k)
338 + (uint64_t)(state->block_p[j * 8 + 5] >> k)
339 + (uint64_t)(state->block_p[j * 8 + 6] >> k)
340 + (uint64_t)(state->block_p[j * 8 + 7] >> k);
343 fs += (uint64_t)(state->block_p[0] >> k);
348 static int count_splitting_option(struct aec_stream *strm)
351 Find the best point for splitting samples in a block.
353 In Rice coding each sample in a block of samples is split at
354 the same position into k LSB and bit_per_sample - k MSB. The
355 LSB part is left binary and the MSB part is coded as a
356 fundamental sequence a.k.a. unary (see CCSDS 121.0-B-2). The
357 function of the length of the Coded Data Set (CDS) depending on
358 k has exactly one minimum (see A. Kiely, IPN Progress Report
361 To find that minimum with only a few costly evaluations of the
362 CDS length, we start with the k of the previous CDS. K is
363 increased and the CDS length evaluated. If the CDS length gets
364 smaller, then we are moving towards the minimum. If the length
365 increases, then the minimum will be found with smaller k.
367 For increasing k we know that we will gain block_size bits in
368 length through the larger binary part. If the FS lenth is less
369 than the block size then a reduced FS part can't compensate the
370 larger binary part. So we know that the CDS for k+1 will be
371 larger than for k without actually computing the length. An
372 analogue check can be done for decreasing k.
376 int this_bs; /* Block size of current block */
377 int no_turn; /* 1 if we shouldn't reverse */
378 int dir; /* Direction, 1 means increasing k, 0 decreasing k */
379 uint64_t len; /* CDS length for current k */
380 uint64_t len_min; /* CDS length minimum so far */
381 uint64_t fs_len; /* Length of FS part (not including 1s) */
383 struct internal_state *state = strm->state;
385 this_bs = strm->block_size - state->ref;
386 len_min = UINT64_MAX;
387 k = k_min = state->k;
388 no_turn = (k == 0) ? 1 : 0;
392 fs_len = block_fs(strm, k);
393 len = fs_len + this_bs * (k + 1);
396 if (len_min < UINT64_MAX)
403 if (fs_len < this_bs || k >= state->kmax) {
413 if (fs_len >= this_bs || k == 0)
430 static int count_se_option(uint64_t limit, struct aec_stream *strm)
434 struct internal_state *state = strm->state;
438 for (i = 0; i < strm->block_size; i+= 2) {
439 d = (uint64_t)state->block_p[i]
440 + (uint64_t)state->block_p[i + 1];
441 /* we have to worry about overflow here */
446 len += d * (d + 1) / 2
447 + (uint64_t)state->block_p[i + 1];
453 static int m_select_code_option(struct aec_stream *strm)
455 uint64_t uncomp_len, split_len, se_len;
456 struct internal_state *state = strm->state;
458 uncomp_len = (strm->block_size - state->ref)
459 * strm->bit_per_sample;
460 split_len = count_splitting_option(strm);
461 se_len = count_se_option(split_len, strm);
463 if (split_len < uncomp_len) {
464 if (split_len < se_len)
465 return m_encode_splitting(strm);
467 return m_encode_se(strm);
469 if (uncomp_len <= se_len)
470 return m_encode_uncomp(strm);
472 return m_encode_se(strm);
476 static int m_encode_splitting(struct aec_stream *strm)
479 struct internal_state *state = strm->state;
482 emit(state, k + 1, state->id_len);
486 emit(state, state->block_p[0], strm->bit_per_sample);
487 for (i = 1; i < strm->block_size; i++)
488 emitfs(state, state->block_p[i] >> k);
489 if (k) emitblock_1(strm, k);
493 for (i = 0; i < strm->block_size; i++)
494 emitfs(state, state->block_p[i] >> k);
495 if (k) emitblock_0(strm, k);
498 return m_flush_block(strm);
501 static int m_encode_uncomp(struct aec_stream *strm)
503 struct internal_state *state = strm->state;
505 emit(state, (1 << state->id_len) - 1, state->id_len);
506 emitblock_0(strm, strm->bit_per_sample);
508 return m_flush_block(strm);
511 static int m_encode_se(struct aec_stream *strm)
515 struct internal_state *state = strm->state;
517 emit(state, 1, state->id_len + 1);
519 emit(state, state->block_p[0], strm->bit_per_sample);
521 for (i = 0; i < strm->block_size; i+= 2) {
522 d = state->block_p[i] + state->block_p[i + 1];
523 emitfs(state, d * (d + 1) / 2 + state->block_p[i + 1]);
526 return m_flush_block(strm);
529 static int m_encode_zero(struct aec_stream *strm)
531 struct internal_state *state = strm->state;
533 emit(state, 0, state->id_len + 1);
536 emit(state, state->zero_ref_sample, strm->bit_per_sample);
538 if (state->zero_blocks == ROS)
540 else if (state->zero_blocks >= 5)
541 emitfs(state, state->zero_blocks);
543 emitfs(state, state->zero_blocks - 1);
545 state->zero_blocks = 0;
546 return m_flush_block(strm);
549 static int m_flush_block(struct aec_stream *strm)
552 Flush block in direct_out mode by updating counters.
554 Fall back to slow flushing if in buffered mode.
557 struct internal_state *state = strm->state;
559 if (state->direct_out) {
560 n = state->cds_p - strm->next_out;
562 strm->avail_out -= n;
563 strm->total_out += n;
564 state->mode = m_get_block;
569 state->mode = m_flush_block_cautious;
573 static int m_flush_block_cautious(struct aec_stream *strm)
576 Slow and restartable flushing
578 struct internal_state *state = strm->state;
580 while(state->cds_buf + state->i < state->cds_p) {
581 if (strm->avail_out == 0)
584 *strm->next_out++ = state->cds_buf[state->i];
589 state->mode = m_get_block;
599 int aec_encode_init(struct aec_stream *strm)
601 struct internal_state *state;
603 if (strm->bit_per_sample > 32 || strm->bit_per_sample == 0)
604 return AEC_CONF_ERROR;
606 if (strm->block_size != 8
607 && strm->block_size != 16
608 && strm->block_size != 32
609 && strm->block_size != 64)
610 return AEC_CONF_ERROR;
612 if (strm->rsi > 4096)
613 return AEC_CONF_ERROR;
615 state = (struct internal_state *)malloc(sizeof(struct internal_state));
617 return AEC_MEM_ERROR;
619 memset(state, 0, sizeof(struct internal_state));
622 if (strm->bit_per_sample > 16) {
623 /* 24/32 input bit settings */
626 if (strm->bit_per_sample <= 24
627 && strm->flags & AEC_DATA_3BYTE) {
628 state->block_len = 3 * strm->block_size;
629 if (strm->flags & AEC_DATA_MSB) {
630 state->get_sample = get_msb_24;
631 state->get_rsi = get_rsi_msb_24;
633 state->get_sample = get_lsb_24;
634 state->get_rsi = get_rsi_lsb_24;
637 state->block_len = 4 * strm->block_size;
638 if (strm->flags & AEC_DATA_MSB) {
639 state->get_sample = get_msb_32;
640 state->get_rsi = get_rsi_msb_32;
642 state->get_sample = get_lsb_32;
643 state->get_rsi = get_rsi_lsb_32;
647 else if (strm->bit_per_sample > 8) {
648 /* 16 bit settings */
650 state->block_len = 2 * strm->block_size;
652 if (strm->flags & AEC_DATA_MSB) {
653 state->get_sample = get_msb_16;
654 state->get_rsi = get_rsi_msb_16;
656 state->get_sample = get_lsb_16;
657 state->get_rsi = get_rsi_lsb_16;
662 state->block_len = strm->block_size;
664 state->get_sample = get_8;
665 state->get_rsi = get_rsi_8;
668 if (strm->flags & AEC_DATA_SIGNED) {
669 state->xmin = -(1ULL << (strm->bit_per_sample - 1));
670 state->xmax = (1ULL << (strm->bit_per_sample - 1)) - 1;
671 state->preprocess = preprocess_signed;
674 state->xmax = (1ULL << strm->bit_per_sample) - 1;
675 state->preprocess = preprocess_unsigned;
678 state->kmax = (1U << state->id_len) - 3;
680 state->block_buf = (uint32_t *)malloc(strm->rsi
683 if (state->block_buf == NULL)
684 return AEC_MEM_ERROR;
686 state->block_p = state->block_buf;
688 /* Largest possible CDS according to specs */
689 state->cds_len = (5 + 64 * 32) / 8 + 3;
690 state->cds_buf = (uint8_t *)malloc(state->cds_len);
691 if (state->cds_buf == NULL)
692 return AEC_MEM_ERROR;
697 state->cds_p = state->cds_buf;
700 state->mode = m_get_block;
705 int aec_encode(struct aec_stream *strm, int flush)
708 Finite-state machine implementation of the adaptive entropy
712 struct internal_state *state;
714 state->flush = flush;
716 while (state->mode(strm) == M_CONTINUE);
718 if (state->direct_out) {
719 n = state->cds_p - strm->next_out;
721 strm->avail_out -= n;
722 strm->total_out += n;
724 *state->cds_buf = *state->cds_p;
725 state->cds_p = state->cds_buf;
726 state->direct_out = 0;
731 int aec_encode_end(struct aec_stream *strm)
733 struct internal_state *state = strm->state;
735 free(state->block_buf);
736 free(state->cds_buf);
741 int aec_buf_encode(struct aec_stream *strm)
745 status = aec_encode_init(strm);
746 if (status != AEC_OK)
748 status = aec_encode(strm, AEC_FLUSH);
749 if (strm->avail_in > 0 || strm->avail_out == 0)
750 status = AEC_DATA_ERROR;
752 aec_encode_end(strm);