1 /**
2  * @file encode.h
3  *
4  * @section LICENSE
5  * Copyright 2021 Mathis Rosenhauer, Moritz Hanke, Joerg Behrens, Luis Kornblueh
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above
15  *    copyright notice, this list of conditions and the following
16  *    disclaimer in the documentation and/or other materials provided
17  *    with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * @section DESCRIPTION
33  *
34  * Adaptive Entropy Encoder
35  * Based on the CCSDS recommended standard 121.0-B-3
36  *
37  */
38 
39 #ifndef ENCODE_H
40 #define ENCODE_H 1
41 
42 #include "config.h"
43 #include <stdint.h>
44 
45 #define M_CONTINUE 1
46 #define M_EXIT 0
47 #define MIN(a, b) (((a) < (b))? (a): (b))
48 
49 /* Maximum CDS length in bytes: 5 bits ID, 64 * 32 bits samples, 7
50  * bits carry from previous CDS */
51 #define CDSLEN ((5 + 64 * 32 + 7 + 7) / 8)
52 
53 /* Marker for Remainder Of Segment condition in zero block encoding */
54 #define ROS -1
55 
56 struct aec_stream;
57 
58 struct internal_state {
59     int (*mode)(struct aec_stream *);
60     uint32_t (*get_sample)(struct aec_stream *);
61     void (*get_rsi)(struct aec_stream *);
62     void (*preprocess)(struct aec_stream *);
63 
64     /* bit length of code option identification key */
65     int id_len;
66 
67     /* minimum integer for preprocessing */
68     uint32_t xmin;
69 
70     /* maximum integer for preprocessing */
71     uint32_t xmax;
72 
73     uint32_t i;
74 
75     /* RSI blocks of preprocessed input */
76     uint32_t *data_pp;
77 
78     /* RSI blocks of input */
79     uint32_t *data_raw;
80 
81     /* remaining blocks in buffer */
82     int blocks_avail;
83 
84     /* blocks encoded so far in RSI */
85     int blocks_dispensed;
86 
87     /* current (preprocessed) input block */
88     uint32_t *block;
89 
90     /* reference sample interval in byte */
91     uint32_t rsi_len;
92 
93     /* current Coded Data Set output */
94     uint8_t *cds;
95 
96     /* buffer for one CDS (only used if strm->next_out cannot hold
97      * full CDS) */
98     uint8_t cds_buf[CDSLEN];
99 
100     /* cds points to strm->next_out (1) or cds_buf (0) */
101     int direct_out;
102 
103     /* Free bits (LSB) in output buffer or accumulator */
104     int bits;
105 
106     /* length of reference sample in current block i.e. 0 or 1
107      * depending on whether the block has a reference sample or not */
108     int ref;
109 
110     /* reference sample stored here for performance reasons */
111     uint32_t ref_sample;
112 
113     /* current zero block has a reference sample */
114     int zero_ref;
115 
116     /* reference sample of zero block */
117     uint32_t zero_ref_sample;
118 
119     /* storage size of samples in bytes */
120     uint32_t bytes_per_sample;
121 
122     /* number of contiguous zero blocks */
123     int zero_blocks;
124 
125     /* 1 if this is the first non-zero block after one or more zero
126      * blocks */
127     int block_nonzero;
128 
129     /* splitting position */
130     int k;
131 
132     /* maximum number for k depending on id_len */
133     int kmax;
134 
135     /* flush option copied from argument */
136     int flush;
137 
138     /* 1 if flushing was successful */
139     int flushed;
140 
141     /* length of uncompressed CDS */
142     uint32_t uncomp_len;
143 };
144 
145 #endif /* ENCODE_H */
146