/src/cmp_tool/lib/common/cmp_entity.h
Line | Count | Source (jump to first uncovered line) |
1 | | /** |
2 | | * @file cmp_entity.h |
3 | | * @author Dominik Loidolt (dominik.loidolt@univie.ac.at) |
4 | | * @date May, 2021 |
5 | | * |
6 | | * @copyright GPLv2 |
7 | | * This program is free software; you can redistribute it and/or modify it |
8 | | * under the terms and conditions of the GNU General Public License, |
9 | | * version 2, as published by the Free Software Foundation. |
10 | | * |
11 | | * This program is distributed in the hope it will be useful, but WITHOUT |
12 | | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
13 | | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
14 | | * more details. |
15 | | * |
16 | | * @brief functions and definition to handle a compression entity |
17 | | * @see Data Compression User Manual PLATO-UVIE-PL-UM-0001 |
18 | | * |
19 | | * @note this code can also used on a little endian machine |
20 | | * |
21 | | * @warning: If you create an entity of one data product type and use get/set |
22 | | * functions intended for another data product type, it will result in a |
23 | | * corrupted entity or garbage data. Do not do this. |
24 | | */ |
25 | | |
26 | | |
27 | | #ifndef CMP_ENTITY_H |
28 | | #define CMP_ENTITY_H |
29 | | |
30 | | #include <stdint.h> |
31 | | |
32 | | #include "compiler.h" |
33 | | #include "cmp_support.h" |
34 | | |
35 | | |
36 | 4.26k | #define GENERIC_HEADER_SIZE 32 |
37 | 0 | #define SPECIFIC_IMAGETTE_HEADER_SIZE 4 |
38 | 0 | #define SPECIFIC_IMAGETTE_ADAPTIVE_HEADER_SIZE 12 |
39 | 3.89k | #define SPECIFIC_NON_IMAGETTE_HEADER_SIZE 32 |
40 | | |
41 | | #define IMAGETTE_HEADER_SIZE \ |
42 | 0 | (GENERIC_HEADER_SIZE + SPECIFIC_IMAGETTE_HEADER_SIZE) |
43 | | #define IMAGETTE_ADAPTIVE_HEADER_SIZE \ |
44 | 0 | (GENERIC_HEADER_SIZE + SPECIFIC_IMAGETTE_ADAPTIVE_HEADER_SIZE) |
45 | | #define NON_IMAGETTE_HEADER_SIZE \ |
46 | 3.89k | (GENERIC_HEADER_SIZE + SPECIFIC_NON_IMAGETTE_HEADER_SIZE) |
47 | | |
48 | 442 | #define CMP_ENTITY_MAX_SIZE 0xFFFFFFUL |
49 | 1.38k | #define CMP_ENTITY_MAX_ORIGINAL_SIZE 0xFFFFFFUL |
50 | | |
51 | 109 | #define RAW_BIT_DATA_TYPE_POS 15U |
52 | | |
53 | 0 | #define CMP_TOOL_VERSION_ID_BIT 0x80000000U |
54 | | |
55 | | |
56 | | /** |
57 | | * @brief PALTO CUC timestamp format |
58 | | */ |
59 | | |
60 | | __extension__ |
61 | | struct timestamp_cmp_ent { |
62 | | uint32_t coarse; |
63 | | uint16_t fine; |
64 | | } __attribute__((packed)); |
65 | | |
66 | | |
67 | | /** |
68 | | * @brief imagette specific part of compression entity |
69 | | */ |
70 | | |
71 | | __extension__ |
72 | | struct imagette_header { |
73 | | uint16_t spill_used; /**< Spillover threshold used */ |
74 | | uint8_t golomb_par_used; /**< Golomb parameter used */ |
75 | | union{ |
76 | | struct { |
77 | | uint8_t spare1; |
78 | | } __attribute__((packed)); |
79 | | struct { |
80 | | uint16_t ap1_spill_used; /**< Adaptive Spillover threshold used 1 */ |
81 | | uint8_t ap1_golomb_par_used; /**< Adaptive Golomb parameter used 1 */ |
82 | | uint16_t ap2_spill_used; /**< Adaptive Spillover threshold used 2 */ |
83 | | uint8_t ap2_golomb_par_used; /**< Adaptive Golomb parameter used 2 */ |
84 | | uint8_t spare2; |
85 | | uint16_t spare3; |
86 | | } __attribute__((packed)); |
87 | | }; |
88 | | } __attribute__((packed)); |
89 | | compile_time_assert(sizeof(struct imagette_header) == SPECIFIC_IMAGETTE_ADAPTIVE_HEADER_SIZE, AP_IMAGETTE_HEADER_T_SIZE_IS_NOT_CORRECT); |
90 | | |
91 | | |
92 | | /** |
93 | | * @brief non-imagette specific part of compression entity |
94 | | */ |
95 | | |
96 | | __extension__ |
97 | | struct non_imagette_header { |
98 | | uint32_t spill_1_used:24; /**< spillover threshold 1 used */ |
99 | | uint16_t cmp_par_1_used; /**< compression parameter 1 used */ |
100 | | uint32_t spill_2_used:24; /**< spillover threshold 2 used */ |
101 | | uint16_t cmp_par_2_used; /**< compression parameter 2 used */ |
102 | | uint32_t spill_3_used:24; /**< spillover threshold 3 used */ |
103 | | uint16_t cmp_par_3_used; /**< compression parameter 3 used */ |
104 | | uint32_t spill_4_used:24; /**< spillover threshold 4 used */ |
105 | | uint16_t cmp_par_4_used; /**< compression parameter 4 used */ |
106 | | uint32_t spill_5_used:24; /**< spillover threshold 5 used */ |
107 | | uint16_t cmp_par_5_used; /**< compression parameter 5 used */ |
108 | | uint32_t spill_6_used:24; /**< spillover threshold 6 used */ |
109 | | uint16_t cmp_par_6_used; /**< compression parameter 6 used */ |
110 | | uint16_t spare; |
111 | | } __attribute__((packed)); |
112 | | compile_time_assert(sizeof(struct non_imagette_header) == SPECIFIC_NON_IMAGETTE_HEADER_SIZE, NON_IMAGETTE_HEADER_T_SIZE_IS_NOT_CORRECT); |
113 | | |
114 | | |
115 | | /** |
116 | | * @brief definition of the compression entity format |
117 | | */ |
118 | | |
119 | | __extension__ |
120 | | struct cmp_entity { |
121 | | uint32_t version_id; /**< ICU ASW/cmp_tool Version ID */ |
122 | | uint32_t cmp_ent_size:24; /**< Compression Entity Size */ |
123 | | uint32_t original_size:24; /**< Original Data Size */ |
124 | | union { |
125 | | uint64_t start_timestamp:48; /**< Compression Start Timestamp */ |
126 | | struct timestamp_cmp_ent start_time; |
127 | | } __attribute__((packed)); |
128 | | union { |
129 | | uint64_t end_timestamp:48; /**< Compression End Timestamp */ |
130 | | struct timestamp_cmp_ent end_time; |
131 | | } __attribute__((packed)); |
132 | | uint16_t data_type; /**< Data Product Type */ |
133 | | uint8_t cmp_mode_used; /**< used Compression Mode */ |
134 | | uint8_t model_value_used; /**< used Model Updating Weighing Value */ |
135 | | uint16_t model_id; /**< Model ID */ |
136 | | uint8_t model_counter; /**< Model Counter */ |
137 | | uint8_t reserved; |
138 | | uint16_t lossy_cmp_par_used; /**< used Lossy Compression Parameters */ |
139 | | union { /* specific Compression Entity Header for the different Data Product Types */ |
140 | | struct imagette_header ima; |
141 | | struct non_imagette_header non_ima; |
142 | | }; |
143 | | } __attribute__((packed)); |
144 | | compile_time_assert(sizeof(struct cmp_entity) == NON_IMAGETTE_HEADER_SIZE, CMP_ENTITY_SIZE_IS_NOT_CORRECT); |
145 | | |
146 | | |
147 | | |
148 | | /* |
149 | | * create a compression entity by setting the size of the compression entity and |
150 | | * the data product type in the entity header |
151 | | */ |
152 | | uint32_t cmp_ent_create(struct cmp_entity *ent, enum cmp_data_type data_type, |
153 | | int raw_mode_flag, uint32_t cmp_size_byte); |
154 | | |
155 | | /* |
156 | | * write the parameters from the RDCU decompression information structure in the |
157 | | * compression entity header |
158 | | */ |
159 | | int cmp_ent_write_rdcu_cmp_pars(struct cmp_entity *ent, const struct cmp_info *info, |
160 | | const struct rdcu_cfg *rcfg); |
161 | | |
162 | | |
163 | | /* set functions for generic compression entity header */ |
164 | | int cmp_ent_set_version_id(struct cmp_entity *ent, uint32_t version_id); |
165 | | int cmp_ent_set_size(struct cmp_entity *ent, uint32_t cmp_ent_size); |
166 | | |
167 | | int cmp_ent_set_original_size(struct cmp_entity *ent, uint32_t original_size); |
168 | | |
169 | | int cmp_ent_set_start_timestamp(struct cmp_entity *ent, uint64_t start_timestamp); |
170 | | int cmp_ent_set_coarse_start_time(struct cmp_entity *ent, uint32_t coarse_time); |
171 | | int cmp_ent_set_fine_start_time(struct cmp_entity *ent, uint16_t fine_time); |
172 | | |
173 | | int cmp_ent_set_end_timestamp(struct cmp_entity *ent, uint64_t end_timestamp); |
174 | | int cmp_ent_set_coarse_end_time(struct cmp_entity *ent, uint32_t coarse_time); |
175 | | int cmp_ent_set_fine_end_time(struct cmp_entity *ent, uint16_t fine_time); |
176 | | |
177 | | int cmp_ent_set_data_type(struct cmp_entity *ent, enum cmp_data_type data_type, |
178 | | int raw_mode_flag); |
179 | | int cmp_ent_set_cmp_mode(struct cmp_entity *ent, enum cmp_mode cmp_mode_used); |
180 | | int cmp_ent_set_model_value(struct cmp_entity *ent, uint32_t model_value_used); |
181 | | int cmp_ent_set_model_id(struct cmp_entity *ent, uint32_t model_id); |
182 | | int cmp_ent_set_model_counter(struct cmp_entity *ent, uint32_t model_counter); |
183 | | int cmp_ent_set_reserved(struct cmp_entity *ent, uint8_t reserved); |
184 | | int cmp_ent_set_lossy_cmp_par(struct cmp_entity *ent, uint32_t lossy_cmp_par_used); |
185 | | |
186 | | |
187 | | /* |
188 | | * set functions for specific entity header for imagette and adaptive imagette |
189 | | * data product types |
190 | | */ |
191 | | int cmp_ent_set_ima_spill(struct cmp_entity *ent, uint32_t spill_used); |
192 | | int cmp_ent_set_ima_golomb_par(struct cmp_entity *ent, uint32_t golomb_par_used); |
193 | | |
194 | | |
195 | | /* |
196 | | * set functions for specific entity header for adaptive imagette data product |
197 | | * types |
198 | | */ |
199 | | int cmp_ent_set_ima_ap1_spill(struct cmp_entity *ent, uint32_t ap1_spill_used); |
200 | | int cmp_ent_set_ima_ap1_golomb_par(struct cmp_entity *ent, uint32_t ap1_golomb_par_used); |
201 | | |
202 | | int cmp_ent_set_ima_ap2_spill(struct cmp_entity *ent, uint32_t ap2_spill_used); |
203 | | int cmp_ent_set_ima_ap2_golomb_par(struct cmp_entity *ent, uint32_t ap2_golomb_par_used); |
204 | | |
205 | | |
206 | | /* set functions for specific entity header for non-imagette data product types */ |
207 | | int cmp_ent_set_non_ima_spill1(struct cmp_entity *ent, uint32_t spill_1_used); |
208 | | int cmp_ent_set_non_ima_cmp_par1(struct cmp_entity *ent, uint32_t cmp_par_1_used); |
209 | | |
210 | | int cmp_ent_set_non_ima_spill2(struct cmp_entity *ent, uint32_t spill_2_used); |
211 | | int cmp_ent_set_non_ima_cmp_par2(struct cmp_entity *ent, uint32_t cmp_par_2_used); |
212 | | |
213 | | int cmp_ent_set_non_ima_spill3(struct cmp_entity *ent, uint32_t spill_3_used); |
214 | | int cmp_ent_set_non_ima_cmp_par3(struct cmp_entity *ent, uint32_t cmp_par_3_used); |
215 | | |
216 | | int cmp_ent_set_non_ima_spill4(struct cmp_entity *ent, uint32_t spill_4_used); |
217 | | int cmp_ent_set_non_ima_cmp_par4(struct cmp_entity *ent, uint32_t cmp_par_4_used); |
218 | | |
219 | | int cmp_ent_set_non_ima_spill5(struct cmp_entity *ent, uint32_t spill_5_used); |
220 | | int cmp_ent_set_non_ima_cmp_par5(struct cmp_entity *ent, uint32_t cmp_par_5_used); |
221 | | |
222 | | int cmp_ent_set_non_ima_spill6(struct cmp_entity *ent, uint32_t spill_6_used); |
223 | | int cmp_ent_set_non_ima_cmp_par6(struct cmp_entity *ent, uint32_t cmp_par_6_used); |
224 | | |
225 | | |
226 | | |
227 | | /* get functions for generic compression entity header */ |
228 | | uint32_t cmp_ent_get_version_id(const struct cmp_entity *ent); |
229 | | uint32_t cmp_ent_get_size(const struct cmp_entity *ent); |
230 | | uint32_t cmp_ent_get_original_size(const struct cmp_entity *ent); |
231 | | |
232 | | uint64_t cmp_ent_get_start_timestamp(const struct cmp_entity *ent); |
233 | | uint32_t cmp_ent_get_coarse_start_time(const struct cmp_entity *ent); |
234 | | uint16_t cmp_ent_get_fine_start_time(const struct cmp_entity *ent); |
235 | | |
236 | | uint64_t cmp_ent_get_end_timestamp(const struct cmp_entity *ent); |
237 | | uint32_t cmp_ent_get_coarse_end_time(const struct cmp_entity *ent); |
238 | | uint16_t cmp_ent_get_fine_end_time(const struct cmp_entity *ent); |
239 | | |
240 | | enum cmp_data_type cmp_ent_get_data_type(const struct cmp_entity *ent); |
241 | | int cmp_ent_get_data_type_raw_bit(const struct cmp_entity *ent); |
242 | | uint8_t cmp_ent_get_cmp_mode(const struct cmp_entity *ent); |
243 | | uint8_t cmp_ent_get_model_value(const struct cmp_entity *ent); |
244 | | |
245 | | uint16_t cmp_ent_get_model_id(const struct cmp_entity *ent); |
246 | | uint8_t cmp_ent_get_model_counter(const struct cmp_entity *ent); |
247 | | uint8_t cmp_ent_get_reserved(const struct cmp_entity *ent); |
248 | | uint16_t cmp_ent_get_lossy_cmp_par(const struct cmp_entity *ent); |
249 | | |
250 | | |
251 | | /* |
252 | | * get functions for specific entity header for imagette and adaptive imagette |
253 | | * data product types |
254 | | */ |
255 | | uint16_t cmp_ent_get_ima_spill(const struct cmp_entity *ent); |
256 | | uint8_t cmp_ent_get_ima_golomb_par(const struct cmp_entity *ent); |
257 | | |
258 | | |
259 | | /* |
260 | | * get functions for specific entity header for adaptive imagette data product |
261 | | * types |
262 | | */ |
263 | | uint16_t cmp_ent_get_ima_ap1_spill(const struct cmp_entity *ent); |
264 | | uint8_t cmp_ent_get_ima_ap1_golomb_par(const struct cmp_entity *ent); |
265 | | |
266 | | uint16_t cmp_ent_get_ima_ap2_spill(const struct cmp_entity *ent); |
267 | | uint8_t cmp_ent_get_ima_ap2_golomb_par(const struct cmp_entity *ent); |
268 | | |
269 | | |
270 | | /* get functions for specific entity header for non-imagette data product types */ |
271 | | uint32_t cmp_ent_get_non_ima_spill1(const struct cmp_entity *ent); |
272 | | uint16_t cmp_ent_get_non_ima_cmp_par1(const struct cmp_entity *ent); |
273 | | |
274 | | uint32_t cmp_ent_get_non_ima_spill2(const struct cmp_entity *ent); |
275 | | uint16_t cmp_ent_get_non_ima_cmp_par2(const struct cmp_entity *ent); |
276 | | |
277 | | uint32_t cmp_ent_get_non_ima_spill3(const struct cmp_entity *ent); |
278 | | uint16_t cmp_ent_get_non_ima_cmp_par3(const struct cmp_entity *ent); |
279 | | |
280 | | uint32_t cmp_ent_get_non_ima_spill4(const struct cmp_entity *ent); |
281 | | uint16_t cmp_ent_get_non_ima_cmp_par4(const struct cmp_entity *ent); |
282 | | |
283 | | uint32_t cmp_ent_get_non_ima_spill5(const struct cmp_entity *ent); |
284 | | uint16_t cmp_ent_get_non_ima_cmp_par5(const struct cmp_entity *ent); |
285 | | |
286 | | uint32_t cmp_ent_get_non_ima_spill6(const struct cmp_entity *ent); |
287 | | uint16_t cmp_ent_get_non_ima_cmp_par6(const struct cmp_entity *ent); |
288 | | |
289 | | |
290 | | /* get function for the compressed data buffer in the entity */ |
291 | | void *cmp_ent_get_data_buf(struct cmp_entity *ent); |
292 | | const void *cmp_ent_get_data_buf_const(const struct cmp_entity *ent); |
293 | | uint32_t cmp_ent_get_cmp_data_size(const struct cmp_entity *ent); |
294 | | int32_t cmp_ent_get_cmp_data(struct cmp_entity *ent, uint32_t *data_buf, |
295 | | uint32_t data_buf_size); |
296 | | |
297 | | /* calculate the size of the compression entity header */ |
298 | | uint32_t cmp_ent_cal_hdr_size(enum cmp_data_type data_type, int raw_mode_flag); |
299 | | |
300 | | |
301 | | #if defined __has_include |
302 | | # if __has_include(<time.h>) |
303 | | # include <time.h> |
304 | | /* create a timestamp for the compression header */ |
305 | | extern const struct tm PLATO_EPOCH_DATE; |
306 | | uint64_t cmp_ent_create_timestamp(const struct timespec *ts); |
307 | | # endif |
308 | | #endif |
309 | | |
310 | | /* print and parse functions */ |
311 | | void cmp_ent_print_header(const struct cmp_entity *ent); |
312 | | void cmp_ent_print_data(struct cmp_entity *ent); |
313 | | void cmp_ent_print(struct cmp_entity *ent); |
314 | | |
315 | | void cmp_ent_parse(struct cmp_entity *ent); |
316 | | |
317 | | #endif /* CMP_ENTITY_H */ |