Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / third_party / om-file-format-sys / c / include / om_decoder.h

om_decoder.h262 lines · 13.2 KiBRaw
1/**
2 * @file om_decoder.h
3 * @brief OmFileDecoder: A component for reading and decompressing data from a compressed file format.
4 *
5 * The OmFileDecoder provides functions and structures for reading chunks of compressed data,
6 * decompressing them, and storing the results into a target buffer. It supports various compression
7 * schemes and allows reading partial or entire datasets in a chunked manner.
8 *
9 * Created by Patrick Zippenfenig on 22.10.2024.
10 */
11
12#ifndef OM_DECODER_H
13#define OM_DECODER_H
14
15#include "om_common.h"
16#include "om_variable.h"
17
18typedef struct {
19 uint64_t lowerBound;
20 uint64_t upperBound;
21} OmRange_t;
22
23typedef struct {
24 uint64_t offset;
25 uint64_t count;
26 OmRange_t indexRange;
27 OmRange_t chunkIndex;
28 OmRange_t nextChunk;
29} OmDecoder_indexRead_t;
30
31typedef OmDecoder_indexRead_t OmDecoder_dataRead_t;
32
33
34typedef struct {
35 /// Number of dimensions
36 uint64_t dimensions_count;
37
38 /// Combine smaller IO reads to a single larger read. If consecutive reads are smaller than `io_size_merge` they might be merged. Default should be 512 bytes.
39 uint64_t io_size_merge;
40
41 /// The maximum IO size before IO operations are split up. Default 64k. If data is in memory, this can be set higher.
42 uint64_t io_size_max;
43
44 /// Each 64 LUT entries are compressed into a LUT chunk. The LUT chunk length returns the size in byte how large a maximum compressed size for a LUT chunk is. 0 for version 1/2 files that do not compress LUT.
45 uint64_t lut_chunk_length;
46
47 /// The offset position where the LUT should start
48 uint64_t lut_start;
49
50 /// uint64_t of data chunks in this file. This value is computed in the initialisation.
51 uint64_t number_of_chunks;
52
53 /// The dimensions of the data array. The last dimension is the "fast" dimension meaning the elements are sequential in memory
54 const uint64_t* dimensions;
55
56 /// The chunk lengths for each dimension
57 const uint64_t* chunks;
58
59 /// The read offset to start reading data in each dimension
60 const uint64_t* read_offset;
61
62 /// How many elements in each dimension to read
63 const uint64_t* read_count;
64
65 /// The dimensions of the target array. This can be the same as `read_count`, but it is also possible to read into a larger array
66 const uint64_t* cube_dimensions;
67
68 /// The offset for each dimension if data is read into a larger array.
69 const uint64_t* cube_offset;
70
71 /// A scalefactor to convert floats to integers
72 float scale_factor;
73
74 /// An offset to convert floats to integers while scaling
75 float add_offset;
76
77 /// The data type of the data
78 uint8_t data_type;
79
80 /// The compression type of the data
81 uint8_t compression;
82
83 /// The size of the elements in bytes
84 uint8_t bytes_per_element;
85
86 /// The size of the elements in bytes after compression, e.g. Int16 could be used to scale floats
87 uint8_t bytes_per_element_compressed;
88} OmDecoder_t;
89
90/**
91 * @brief Initializes the `om_decoder_t` structure with the specified parameters.
92 *
93 * This function sets up the `om_decoder_t` instance, configuring its dimensions, chunk information,
94 * reading parameters, LUT (Look-Up Table) properties, and decompression methods. It prepares the
95 * decoder for reading and processing compressed data.
96 *
97 * @param decoder A pointer to an `om_decoder_t` structure that will be initialized.
98 * @param variable A pointer to the data region of the variable to read
99 * @param dimension_count The number of dimensions of the data (e.g., 3 for 3D data). All following array must have the some dimension count.
100 * @param read_offset A pointer to an array specifying the offsets for reading data in each dimension. This array sets the starting points for data reads.
101 * @param read_count A pointer to an array specifying the number of elements to read along each dimension. It defines how much data to read starting from `read_offset`.
102 * @param cube_offset A pointer to an array specifying the offset of the target cube in each dimension. This is used when reading data into a larger array.
103 * @param cube_dimensions A pointer to an array specifying the dimensions of the target cube being written to.It can be the same as `read_count` but allows writing into larger arrays..
104 * @param io_size_merge The maximum size (in bytes) for merging consecutive IO operations. It helps to optimize read performance by merging small reads.
105 * @param io_size_max The maximum size (in bytes) for a single IO operation before it is split. It defines the threshold for splitting large reads.
106 *
107 * @returns Return an om_error_t if the compression or dimension is invalid
108 */
109OmError_t om_decoder_init(
110 OmDecoder_t* decoder,
111 const OmVariable_t* variable,
112 uint64_t dimension_count,
113 const uint64_t* read_offset,
114 const uint64_t* read_count,
115 const uint64_t* cube_offset,
116 const uint64_t* cube_dimensions,
117 uint64_t io_size_merge,
118 uint64_t io_size_max
119);
120
121//OmError_t OmDecoder_init(OmDecoder_t* decoder, float scalefactor, float add_offset, const OmCompression_t compression, const OmDataType_t data_type, uint64_t dimension_count, const uint64_t* dimensions, const uint64_t* chunks, const uint64_t* read_offset, const uint64_t* read_count, const uint64_t* cube_offset, const uint64_t* cube_dimensions, uint64_t lut_size, uint64_t lut_chunk_element_count, uint64_t lut_start, uint64_t io_size_merge, uint64_t io_size_max);
122
123/**
124 * @brief Initializes an `om_decoder_index_read_t` structure for reading chunk indices.
125 *
126 * This function calculates the starting and ending chunk indices for reading data based on
127 * the dimensions, chunk sizes, and read parameters provided by the `om_decoder_t` structure.
128 * It determines the range of chunks that need to be processed to fulfill the read request,
129 * setting the appropriate values in the `om_decoder_index_read_t` structure.
130 *
131 * @param decoder A pointer to a constant `om_decoder_t` structure containing information
132 * about the data array, chunk sizes, read offsets, and dimensions.
133 * @param index_read A pointer to an `om_decoder_index_read_t` structure that will be
134 * initialized with the computed chunk index range and other related values.
135 */
136void om_decoder_init_index_read(const OmDecoder_t* decoder, OmDecoder_indexRead_t *indexRead);
137
138
139/**
140 * @brief Determines the next range of chunks to be read and updates the `om_decoder_index_read_t` structure.
141 *
142 * This function calculates the next set of chunk indices that need to be read from the data, based on the
143 * limits set by the maximum I/O size, alignment, and the logical structure of the data. It updates the
144 * `om_decoder_index_read_t` structure to reflect the new range of chunks and the corresponding read parameters.
145 *
146 * @param decoder A pointer to a constant `om_decoder_t` structure containing information about the data array,
147 * chunk sizes, I/O constraints, and other relevant parameters.
148 * @param index_read A pointer to an `om_decoder_index_read_t` structure, which will be updated with the next
149 * range of chunks to read and the corresponding offset and size of the read operation.
150 *
151 * @returns `true` if the next read range was successfully computed and updated in `index_read`.
152 * `false` if there are no more chunks left to read, indicating that the end of the read range has been reached.
153 *
154 */
155bool om_decoder_next_index_read(const OmDecoder_t* decoder, OmDecoder_indexRead_t* index_read);
156
157
158/**
159 * @brief Initializes an `om_decoder_data_read_t` structure for data reading.
160 *
161 * This function sets the initial state of the `om_decoder_data_read_t` structure,
162 * preparing it for subsequent data read operations. It initializes the offset,
163 * count, index range, and chunk indices based on the provided index read structure.
164 *
165 * @param[out] data_read A pointer to the `om_decoder_data_read_t` structure that
166 * will be initialized. This structure maintains the state
167 * for reading data chunks.
168 * @param[in] index_read A pointer to the `om_decoder_index_read_t` structure that
169 * contains information about the index range and the initial
170 * chunk index to be used for reading.
171 */
172void om_decoder_init_data_read(OmDecoder_dataRead_t *data_read, const OmDecoder_indexRead_t *index_read);
173
174
175/**
176 * @brief Prepares the next data read operation for a given chunk of compressed data.
177 *
178 * This function advances the data reading process by calculating the start and end positions
179 * of the next data segment to read based on the provided index data and current state
180 * of the `om_decoder_data_read_t` structure. It determines how much data to read from
181 * the compressed data source and where the decompressed data should be written.
182 *
183 * The function supports two formats:
184 * 1. Version 1 format (non-compressed LUTs, where `lut_chunk_element_count` is 1).
185 * 2. Compressed LUT format (Version 3+).
186 *
187 * The function reads from a specified index data and updates the `data_read` structure
188 * with the offset and size of the next data block to be read.
189 *
190 * @param[in] decoder A pointer to the `om_decoder_t` structure, which contains
191 * information about the file format, compression settings,
192 * and LUT (look-up table) characteristics.
193 * @param[out] data_read A pointer to `om_decoder_data_read_t` structure that holds
194 * the state of the current data read operation. This structure
195 * is updated to reflect the next segment of data to read.
196 * @param[in] index_data A pointer to the index data, which provides the compressed
197 * offset information for each chunk in the LUT.
198 * @param[in] index_data_size The size of the `index_data` buffer in bytes.
199 *
200 * @returns `true` if the next data segment was successfully prepared and ready for reading,
201 * `false` if there are no more data segments to read or if the range is exhausted.
202 *
203 * @returns May return an out-of-bounds read error on corrupted data.
204 */
205bool om_decoder_next_data_read(const OmDecoder_t *decoder, OmDecoder_dataRead_t* dataRead, const void* indexData, uint64_t indexDataCount, OmError_t* error);
206
207
208/**
209 * @brief Calculates the size of the buffer required to read a single data chunk.
210 *
211 * This function determines the size, in bytes, of the buffer needed to read a single
212 * chunk of data from the compressed file, based on the chunk dimensions and the
213 * number of bytes per element after decompression.
214 *
215 * The calculation involves multiplying the chunk lengths across all dimensions by
216 * the number of bytes required to store a single element in the decompressed form.
217 *
218 * @param[in] decoder A pointer to the `om_decoder_t` structure, which holds the
219 * decoder configuration, including chunk sizes for each dimension,
220 * and the number of bytes per element.
221 *
222 * @returns The size of the buffer required to read a single chunk, in bytes.
223 *
224 * @note This buffer size is used to allocate memory when reading data chunks during
225 * the decoding process, ensuring that there is sufficient space to store the
226 * decompressed data of a chunk.
227 */
228uint64_t om_decoder_read_buffer_size(const OmDecoder_t* decoder);
229
230
231/**
232 * @brief Decodes multiple data chunks from compressed input into a target buffer.
233 *
234 * This function iteratively decodes a range of chunks from the compressed input data
235 * using the specified decoder configuration. It processes each chunk within the provided
236 * range, decompressing it into a buffer and copying the decompressed data into the
237 * output buffer. The decoding uses an internal function `_om_decoder_decode_chunk`
238 * for each individual chunk.
239 *
240 * @param[in] decoder A pointer to the `om_decoder_t` structure, which defines
241 * the decoder configuration, including dimensions, chunk sizes,
242 * data type and compression type.
243 * @param[in] chunk An `om_range_t` structure specifying the range of chunks
244 * to decode. The range is defined by `chunk.lowerBound`
245 * (inclusive) and `chunk.upperBound` (exclusive).
246 * @param[in] data A pointer to the compressed data buffer from which the chunks
247 * are read and decoded.
248 * @param[in] data_size The size of the `data` buffer, in bytes. This ensures that
249 * the function does not read beyond the provided data size.
250 * @param[out] into A pointer to the output buffer where the decompressed data
251 * will be written. The buffer should have enough space to store
252 * the decompressed output.
253 * @param[out] chunkBuffer A temporary buffer used for storing intermediate decompressed
254 * chunk data before copying it into the final output buffer.
255 * Must be sized according to `om_decoder_read_buffer_size`
256 * @param[out] error May return an out-of-bounds read error on corrupted data.
257 *
258 * @returns `false` if an error occurred.
259 */
260bool om_decoder_decode_chunks(const OmDecoder_t *decoder, OmRange_t chunkIndex, const void *data, uint64_t dataCount, void *into, void *chunkBuffer, OmError_t* error);
261
262#endif // OM_DECODER_H