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

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

om_decoder.c845 lines · 34.4 KiBRaw
1//
2// om_decoder.c
3// OpenMeteoApi
4//
5// Created by Patrick Zippenfenig on 22.10.2024.
6//
7
8#include <assert.h>
9#include "vp4.h"
10#include "fp.h"
11#include "conf.h"
12#include "delta2d.h"
13#include "om_decoder.h"
14
15#pragma clang diagnostic error "-Wswitch"
16
17void om_decoder_init_data_read(OmDecoder_dataRead_t *data_read, const OmDecoder_indexRead_t *index_read) {
18 data_read->offset = 0;
19 data_read->count = 0;
20 data_read->indexRange = index_read->indexRange;
21 data_read->chunkIndex.lowerBound = 0;
22 data_read->chunkIndex.upperBound = 0;
23 data_read->nextChunk = index_read->chunkIndex;
24}
25
26OmError_t om_decoder_init(
27 OmDecoder_t* decoder,
28 const OmVariable_t* variable,
29 uint64_t dimension_count,
30 const uint64_t* read_offset,
31 const uint64_t* read_count,
32 const uint64_t* cube_offset,
33 const uint64_t* cube_dimensions,
34 uint64_t io_size_merge,
35 uint64_t io_size_max
36) {
37
38 float scalefactor, add_offset;
39 const uint64_t *dimensions, *chunks;
40 uint8_t data_type;
41 uint8_t compression;
42 uint64_t lut_size, lut_start, lut_chunk_length;
43
44 switch (_om_variable_memory_layout(variable)) {
45 case OM_MEMORY_LAYOUT_LEGACY: {
46 const OmHeaderV1_t* metaV1 = (const OmHeaderV1_t*)variable;
47 scalefactor = metaV1->scale_factor;
48 add_offset = 0;
49 data_type = DATA_TYPE_FLOAT_ARRAY;
50 compression = metaV1->compression_type;
51 if (metaV1->version == 1) {
52 compression = COMPRESSION_PFOR_DELTA2D_INT16;
53 }
54 lut_chunk_length = 0;
55 lut_start = 40; // Right after header
56 lut_size = 0; // ignored
57 // dim1 follows dim0 in header, thus this pointer is correct
58 dimensions = &metaV1->dim0;
59 // chunk1 follows chunk0 in header, thus this pointer is correct
60 chunks = &metaV1->chunk0;
61 break;
62 }
63 case OM_MEMORY_LAYOUT_ARRAY: {
64 const OmVariableArrayV3_t* metaV3 = (const OmVariableArrayV3_t*)variable;
65 scalefactor = metaV3->scale_factor;
66 add_offset = metaV3->add_offset;
67 data_type = metaV3->data_type;
68 compression = metaV3->compression_type;
69 lut_size = metaV3->lut_size;
70 lut_start = metaV3->lut_offset;
71 dimensions = om_variable_get_dimensions(variable);
72 chunks = om_variable_get_chunks(variable);
73 lut_chunk_length = 1;
74 break;
75 }
76 case OM_MEMORY_LAYOUT_SCALAR:
77 return ERROR_INVALID_DATA_TYPE;
78 }
79
80 // Calculate the number of chunks based on dims and chunks
81 uint64_t nChunks = 1;
82 for (uint64_t i = 0; i < dimension_count; i++) {
83 if (dimensions[i] == 0) {
84 return ERROR_INVALID_DIMENSIONS;
85 }
86 if (chunks[i] == 0 || chunks[i] > dimensions[i]) {
87 return ERROR_INVALID_CHUNK_DIMENSIONS;
88 }
89 if (read_offset[i] >= dimensions[i]) {
90 return ERROR_INVALID_READ_OFFSET;
91 }
92 if (read_count[i] > dimensions[i] || read_offset[i] + read_count[i] > dimensions[i]) {
93 return ERROR_INVALID_READ_COUNT;
94 }
95 const uint64_t this_cube_offset = cube_offset == NULL ? 0 : cube_offset[i];
96 const uint64_t this_cube_dimension = cube_dimensions == NULL ? read_count[i] : cube_dimensions[i];
97 if (this_cube_offset + read_count[i] > this_cube_dimension) {
98 return ERROR_INVALID_CUBE_OFFSET;
99 }
100 nChunks *= divide_rounded_up(dimensions[i], chunks[i]);
101 }
102
103 // Correctly calculate number of chunks
104 if (lut_chunk_length > 0) {
105 const uint64_t nLutChunks = divide_rounded_up(nChunks+1, LUT_CHUNK_COUNT);
106 lut_chunk_length = lut_size / nLutChunks;
107 }
108
109 decoder->number_of_chunks = nChunks;
110 decoder->scale_factor = scalefactor;
111 decoder->add_offset = add_offset;
112 decoder->dimensions = dimensions;
113 decoder->dimensions_count = dimension_count;
114 decoder->chunks = chunks;
115 decoder->read_offset = read_offset;
116 decoder->read_count = read_count;
117 decoder->cube_offset = cube_offset;
118 decoder->cube_dimensions = cube_dimensions;
119 decoder->lut_chunk_length = lut_chunk_length;
120 decoder->lut_start = lut_start;
121 decoder->io_size_merge = io_size_merge;
122 decoder->io_size_max = io_size_max;
123 decoder->data_type = data_type;
124 decoder->compression = compression;
125
126 OmError_t error = ERROR_OK;
127 decoder->bytes_per_element = om_get_bytes_per_element(data_type, &error);
128 decoder->bytes_per_element_compressed = om_get_bytes_per_element_compressed(data_type, compression, &error);
129 return error;
130}
131
132ALWAYS_INLINE uint64_t om_decode_decompress(
133 OmDataType_t data_type,
134 OmCompression_t compression_type,
135 const void* input,
136 uint64_t count,
137 void* output
138) {
139 uint64_t result = 0;
140
141 switch (compression_type) {
142 case COMPRESSION_PFOR_DELTA2D_INT16:
143 case COMPRESSION_PFOR_DELTA2D_INT16_LOGARITHMIC:
144 assert(data_type == DATA_TYPE_FLOAT_ARRAY && "Expecting float array");
145 result = p4nzdec128v16((unsigned char*)input, (size_t)count, (uint16_t*)output);
146 break;
147 case COMPRESSION_FPX_XOR2D:
148 assert(data_type == DATA_TYPE_FLOAT_ARRAY || data_type == DATA_TYPE_DOUBLE_ARRAY && "Expecting float or double array");
149 if (data_type == DATA_TYPE_FLOAT_ARRAY) {
150 result = om_common_decompress_fpxdec32((unsigned char*)input, (size_t)count, (float*)output);
151 } else if (data_type == DATA_TYPE_DOUBLE_ARRAY) {
152 result = om_common_decompress_fpxdec64((unsigned char*)input, (size_t)count, (double*)output);
153 }
154 break;
155 case COMPRESSION_PFOR_DELTA2D:
156 switch (data_type) {
157 case DATA_TYPE_INT8_ARRAY:
158 result = p4nzdec8((unsigned char*)input, (size_t)count, (uint8_t*)output);
159 break;
160 case DATA_TYPE_UINT8_ARRAY:
161 result = p4nddec8((unsigned char*)input, (size_t)count, (uint8_t*)output);
162 break;
163 case DATA_TYPE_INT16_ARRAY:
164 result = p4nzdec128v16((unsigned char*)input, (size_t)count, (uint16_t*)output);
165 break;
166 case DATA_TYPE_UINT16_ARRAY:
167 result = p4nddec128v16((unsigned char*)input, (size_t)count, (uint16_t*)output);
168 break;
169 case DATA_TYPE_INT32_ARRAY:
170 result = p4nzdec128v32((unsigned char*)input, (size_t)count, (uint32_t*)output);
171 break;
172 case DATA_TYPE_UINT32_ARRAY:
173 result = p4nddec128v32((unsigned char*)input, (size_t)count, (uint32_t*)output);
174 break;
175 case DATA_TYPE_INT64_ARRAY:
176 result = p4nzdec64((unsigned char*)input, (size_t)count, (uint64_t*)output);
177 break;
178 case DATA_TYPE_UINT64_ARRAY:
179 result = p4nddec64((unsigned char*)input, (size_t)count, (uint64_t*)output);
180 break;
181 case DATA_TYPE_FLOAT_ARRAY:
182 result = p4nzdec128v32((unsigned char*)input, (size_t)count, (uint32_t*)output);
183 break;
184 case DATA_TYPE_DOUBLE_ARRAY:
185 result = p4nzdec64((unsigned char*)input, (size_t)count, (uint64_t*)output);
186 break;
187 case DATA_TYPE_NONE:
188 case DATA_TYPE_STRING:
189 case DATA_TYPE_STRING_ARRAY:
190 case DATA_TYPE_INT8:
191 case DATA_TYPE_UINT8:
192 case DATA_TYPE_INT16:
193 case DATA_TYPE_UINT16:
194 case DATA_TYPE_INT32:
195 case DATA_TYPE_UINT32:
196 case DATA_TYPE_INT64:
197 case DATA_TYPE_UINT64:
198 case DATA_TYPE_FLOAT:
199 case DATA_TYPE_DOUBLE:
200 break;
201 }
202 break;
203
204 case COMPRESSION_NONE:
205 break;
206 }
207
208 return result;
209}
210
211ALWAYS_INLINE void om_decode_filter(
212 OmDataType_t data_type,
213 OmCompression_t compression_type,
214 void* data,
215 uint64_t length_in_chunk,
216 uint64_t length_last
217) {
218 switch (compression_type) {
219 case COMPRESSION_PFOR_DELTA2D_INT16:
220 case COMPRESSION_PFOR_DELTA2D_INT16_LOGARITHMIC:
221 assert(data_type == DATA_TYPE_FLOAT_ARRAY && "Expecting float array");
222 delta2d_decode16((size_t)(length_in_chunk / length_last), (size_t)length_last, (int16_t*)data);
223 break;
224 case COMPRESSION_FPX_XOR2D:
225 assert(data_type == DATA_TYPE_FLOAT_ARRAY || data_type == DATA_TYPE_DOUBLE_ARRAY && "Expecting float or double array");
226 if (data_type == DATA_TYPE_FLOAT_ARRAY) {
227 delta2d_decode_xor((size_t)(length_in_chunk / length_last), (size_t)length_last, (float*)data);
228 } else if (data_type == DATA_TYPE_DOUBLE_ARRAY) {
229 delta2d_decode_xor_double((size_t)(length_in_chunk / length_last), (size_t)length_last, (double*)data);
230 }
231 break;
232 case COMPRESSION_PFOR_DELTA2D:
233 switch (data_type) {
234 case DATA_TYPE_INT8_ARRAY:
235 case DATA_TYPE_UINT8_ARRAY:
236 delta2d_decode8((size_t)(length_in_chunk / length_last), (size_t)length_last, (int8_t*)data);
237 break;
238 case DATA_TYPE_INT16_ARRAY:
239 case DATA_TYPE_UINT16_ARRAY:
240 delta2d_decode16((size_t)(length_in_chunk / length_last), (size_t)length_last, (int16_t*)data);
241 break;
242 case DATA_TYPE_INT32_ARRAY:
243 case DATA_TYPE_UINT32_ARRAY:
244 case DATA_TYPE_FLOAT_ARRAY:
245 delta2d_decode32((size_t)(length_in_chunk / length_last), (size_t)length_last, (int32_t*)data);
246 break;
247 case DATA_TYPE_INT64_ARRAY:
248 case DATA_TYPE_UINT64_ARRAY:
249 case DATA_TYPE_DOUBLE_ARRAY:
250 delta2d_decode64((size_t)(length_in_chunk / length_last), (size_t)length_last, (int64_t*)data);
251 break;
252 default:
253 break;
254 }
255 break;
256
257 case COMPRESSION_NONE:
258 break;
259 }
260}
261
262ALWAYS_INLINE void om_decode_copy(
263 OmDataType_t data_type,
264 OmCompression_t compression_type,
265 uint64_t count,
266 float scale_factor,
267 float add_offset,
268 const void* input,
269 void* output
270) {
271 switch (compression_type) {
272 case COMPRESSION_PFOR_DELTA2D_INT16:
273 assert(data_type == DATA_TYPE_FLOAT_ARRAY && "Expecting float array");
274 om_common_copy_int16_to_float(count, scale_factor, add_offset, input, output);
275 break;
276
277 case COMPRESSION_PFOR_DELTA2D_INT16_LOGARITHMIC:
278 assert(data_type == DATA_TYPE_FLOAT_ARRAY && "Expecting float array");
279 om_common_copy_int16_to_float_log10(count, scale_factor, input, output);
280 break;
281
282 case COMPRESSION_FPX_XOR2D:
283 assert(data_type == DATA_TYPE_FLOAT_ARRAY || data_type == DATA_TYPE_DOUBLE_ARRAY && "Expecting float or double array");
284 if (data_type == DATA_TYPE_FLOAT_ARRAY) {
285 om_common_copy32(count, input, output);
286 } else if (data_type == DATA_TYPE_DOUBLE_ARRAY) {
287 om_common_copy64(count, input, output);
288 }
289 break;
290
291 case COMPRESSION_PFOR_DELTA2D:
292 switch (data_type) {
293 case DATA_TYPE_INT8_ARRAY:
294 case DATA_TYPE_UINT8_ARRAY:
295 om_common_copy8(count, input, output);
296 break;
297 case DATA_TYPE_INT16_ARRAY:
298 case DATA_TYPE_UINT16_ARRAY:
299 om_common_copy16(count, input, output);
300 break;
301 case DATA_TYPE_INT32_ARRAY:
302 case DATA_TYPE_UINT32_ARRAY:
303 om_common_copy32(count, input, output);
304 break;
305 case DATA_TYPE_FLOAT_ARRAY:
306 om_common_copy_int32_to_float(count, scale_factor, add_offset, input, output);
307 break;
308 case DATA_TYPE_INT64_ARRAY:
309 case DATA_TYPE_UINT64_ARRAY:
310 om_common_copy64(count, input, output);
311 break;
312 case DATA_TYPE_DOUBLE_ARRAY:
313 om_common_copy_int64_to_double(count, scale_factor, add_offset, input, output);
314 break;
315 default:
316 break;
317 }
318 break;
319 case COMPRESSION_NONE:
320 break;
321 }
322}
323
324void om_decoder_init_index_read(const OmDecoder_t* decoder, OmDecoder_indexRead_t *index_read) {
325 uint64_t chunkStart = 0;
326 uint64_t chunkEnd = 1;
327
328 for (uint64_t i = 0; i < decoder->dimensions_count; i++) {
329 const uint64_t dimension = decoder->dimensions[i];
330 const uint64_t chunk = decoder->chunks[i];
331 const uint64_t read_offset = decoder->read_offset[i];
332 const uint64_t read_count = decoder->read_count[i];
333 //printf("dimension=%llu chunk=%llu read_offset=%llu read_count=%llu\n", dimension,chunk,read_offset,read_count);
334
335 // Calculate lower and upper chunk indices for the current dimension
336 const uint64_t chunkInThisDimensionLower = read_offset / chunk;
337 const uint64_t chunkInThisDimensionUpper = divide_rounded_up(read_offset + read_count, chunk);
338 const uint64_t chunkInThisDimensionCount = chunkInThisDimensionUpper - chunkInThisDimensionLower;
339
340 const uint64_t firstChunkInThisDimension = chunkInThisDimensionLower;
341 const uint64_t nChunksInThisDimension = divide_rounded_up(dimension, chunk);
342
343 // Update chunkStart and chunkEnd
344 chunkStart = chunkStart * nChunksInThisDimension + firstChunkInThisDimension;
345
346 if (read_count == dimension) {
347 // The entire dimension is read
348 chunkEnd = chunkEnd * nChunksInThisDimension;
349 } else {
350 // Only parts of this dimension are read
351 chunkEnd = chunkStart + chunkInThisDimensionCount;
352 }
353 }
354 index_read->offset = 0;
355 index_read->count = 0;
356 index_read->indexRange.lowerBound = 0;
357 index_read->indexRange.upperBound = 0;
358 index_read->chunkIndex.lowerBound = 0;
359 index_read->chunkIndex.upperBound = 0;
360 index_read->nextChunk.lowerBound = chunkStart;
361 index_read->nextChunk.upperBound = chunkEnd;
362}
363
364uint64_t om_decoder_read_buffer_size(const OmDecoder_t* decoder) {
365 uint64_t chunkLength = 1;
366 for (uint64_t i = 0; i < decoder->dimensions_count; i++) {
367 chunkLength *= decoder->chunks[i];
368 }
369 return chunkLength * decoder->bytes_per_element;
370}
371
372bool _om_decoder_next_chunk_position(const OmDecoder_t *decoder, OmRange_t *chunk_index) {
373 uint64_t rollingMultiply = 1;
374
375 // Number of consecutive chunks that can be read linearly.
376 uint64_t linearReadCount = 1;
377 bool linearRead = true;
378 const uint64_t dimensions_count = decoder->dimensions_count;
379
380 for (uint64_t i_forward = 0; i_forward < dimensions_count; i_forward++) {
381 const uint64_t i = dimensions_count - i_forward - 1;
382 const uint64_t dimension = decoder->dimensions[i];
383 const uint64_t chunk = decoder->chunks[i];
384 const uint64_t read_offset = decoder->read_offset[i];
385 const uint64_t read_count = decoder->read_count[i];
386
387 // Number of chunks in this dimension.
388 const uint64_t nChunksInThisDimension = divide_rounded_up(dimension, chunk);
389
390 // Calculate chunk range in this dimension.
391 const uint64_t chunkInThisDimensionLower = read_offset / chunk;
392 const uint64_t chunkInThisDimensionUpper = divide_rounded_up(read_offset + read_count, chunk);
393 const uint64_t chunkInThisDimensionCount = chunkInThisDimensionUpper - chunkInThisDimensionLower;
394
395 // Move forward by one.
396 chunk_index->lowerBound += rollingMultiply;
397
398 // Check for linear read conditions.
399 if (i == dimensions_count - 1 && dimension != read_count) {
400 // If the fast dimension is only partially read.
401 linearReadCount = chunkInThisDimensionCount;
402 linearRead = false;
403 }
404
405 if (linearRead && dimension == read_count) {
406 // The dimension is read entirely.
407 linearReadCount *= nChunksInThisDimension;
408 } else {
409 // Dimension is read partly; cannot merge further reads.
410 linearRead = false;
411 }
412
413 // Calculate the chunk index in this dimension.
414 uint64_t c0 = (chunk_index->lowerBound / rollingMultiply) % nChunksInThisDimension;
415
416 // Check for overflow.
417 if (c0 != chunkInThisDimensionUpper && c0 != 0) {
418 break; // No overflow in this dimension, break.
419 }
420
421 // Adjust chunkIndex.lowerBound if there is an overflow.
422 chunk_index->lowerBound -= chunkInThisDimensionCount * rollingMultiply;
423
424 // Update the rolling multiplier for the next dimension.
425 rollingMultiply *= nChunksInThisDimension;
426
427 // If we're at the first dimension and have processed all chunks.
428 if (i == 0) {
429 chunk_index->upperBound = chunk_index->lowerBound;
430 return false;
431 }
432 }
433
434 // Update chunkIndex.upperBound based on the number of chunks that can be read linearly.
435 chunk_index->upperBound = chunk_index->lowerBound + linearReadCount;
436 return true;
437}
438
439bool om_decoder_next_index_read(const OmDecoder_t* decoder, OmDecoder_indexRead_t* index_read) {
440 if (index_read->nextChunk.lowerBound >= index_read->nextChunk.upperBound) {
441 return false;
442 }
443
444 index_read->chunkIndex = index_read->nextChunk;
445 index_read->indexRange.lowerBound = index_read->nextChunk.lowerBound;
446
447 uint64_t chunkIndex = index_read->nextChunk.lowerBound;
448
449 const bool isV3LUT = decoder->lut_chunk_length > 1;
450 const uint64_t lut_chunk_element_count = isV3LUT ? LUT_CHUNK_COUNT : 1;
451 const uint64_t lut_chunk_length = isV3LUT ? decoder->lut_chunk_length : sizeof(uint64_t);
452 const uint64_t io_size_max = decoder->io_size_max;
453
454 const uint64_t alignOffset = isV3LUT || index_read->indexRange.lowerBound == 0 ? 0 : 1;
455 const uint64_t endAlignOffset = isV3LUT ? 1 : 0;
456
457 const uint64_t readStart = (index_read->nextChunk.lowerBound - alignOffset) / lut_chunk_element_count * lut_chunk_length;
458
459 while (true) {
460 const uint64_t maxRead = io_size_max / lut_chunk_length * lut_chunk_element_count;
461 const uint64_t nextChunkCount = index_read->nextChunk.upperBound - index_read->nextChunk.lowerBound;
462 const uint64_t nextIncrement = om_max(1, om_min(maxRead - 1, nextChunkCount - 1));
463
464 if (index_read->nextChunk.lowerBound + nextIncrement >= index_read->nextChunk.upperBound) {
465 if (!_om_decoder_next_chunk_position(decoder, &index_read->nextChunk)) {
466 break;
467 }
468 const uint64_t readEndNext = divide_rounded_up(index_read->nextChunk.lowerBound + endAlignOffset, lut_chunk_element_count) * lut_chunk_length;
469 const uint64_t readStartNext = readEndNext - lut_chunk_length;
470 const uint64_t readEndPrevious = chunkIndex / lut_chunk_element_count * lut_chunk_length;
471
472 if (readEndNext - readStart > io_size_max) {
473 break;
474 }
475 if (readStartNext - readEndPrevious > decoder->io_size_merge) {
476 break;
477 }
478 } else {
479 const uint64_t readEndNext = (index_read->nextChunk.lowerBound + nextIncrement + endAlignOffset) / lut_chunk_element_count * lut_chunk_length;
480
481 if (readEndNext - readStart > io_size_max) {
482 index_read->nextChunk.lowerBound += 1;
483 break;
484 }
485 index_read->nextChunk.lowerBound += nextIncrement;
486 }
487 chunkIndex = index_read->nextChunk.lowerBound;
488 }
489
490 const uint64_t readEnd = ((chunkIndex + endAlignOffset) / lut_chunk_element_count + 1) * lut_chunk_length;
491 //uint64_t lutTotalSize = divide_rounded_up(decoder->number_of_chunks, decoder->lut_chunk_element_count) * lut_chunk_length;
492 //assert(readEnd <= lutTotalSize);
493
494 index_read->offset = decoder->lut_start + readStart;
495 index_read->count = readEnd - readStart;
496 index_read->indexRange.upperBound = chunkIndex + 1;
497 return true;
498}
499
500bool om_decoder_next_data_read(const OmDecoder_t *decoder, OmDecoder_dataRead_t* data_read, const void* index_data, uint64_t index_data_size, OmError_t* error) {
501 if (data_read->nextChunk.lowerBound >= data_read->nextChunk.upperBound) {
502 return false;
503 }
504
505 uint64_t chunkIndex = data_read->nextChunk.lowerBound;
506 data_read->chunkIndex.lowerBound = chunkIndex;
507
508 const uint64_t number_of_chunks = decoder->number_of_chunks;
509
510 // Version 1 case
511 if (decoder->lut_chunk_length == 0) {
512 // index is a flat Int64 array
513 const uint64_t* data = (const uint64_t*)index_data;
514
515 const bool isOffset0 = (data_read->indexRange.lowerBound == 0);
516 const uint64_t startOffset = isOffset0 ? 1 : 0;
517
518
519 uint64_t readPos = chunkIndex - data_read->indexRange.lowerBound - startOffset;
520 //printf("chunkIndex %llu lowerBound %llu readPos %llu \n", chunkIndex, data_read->indexRange.lowerBound, readPos);
521 if (!isOffset0 && (readPos + 1) * sizeof(int64_t) > index_data_size) {
522 (*error) = ERROR_OUT_OF_BOUND_READ;
523 return false;
524 }
525
526 const uint64_t startPos = isOffset0 && chunkIndex == 0 ? 0 : data[readPos];
527 uint64_t endPos = startPos;
528
529 // Loop to the next chunk until the end is reached
530 while (true) {
531 readPos = data_read->nextChunk.lowerBound - data_read->indexRange.lowerBound - startOffset + 1;
532 if ((readPos + 1) * sizeof(int64_t) > index_data_size) {
533 (*error) = ERROR_OUT_OF_BOUND_READ;
534 return false;
535 }
536 const uint64_t dataEndPos = data[readPos];
537
538 // Merge and split IO requests, ensuring at least one IO request is sent
539 if (startPos != endPos && (dataEndPos - startPos > decoder->io_size_max || dataEndPos - endPos > decoder->io_size_merge)) {
540 break;
541 }
542 endPos = dataEndPos;
543 chunkIndex = data_read->nextChunk.lowerBound;
544
545 if (data_read->nextChunk.lowerBound + 1 >= data_read->nextChunk.upperBound) {
546 if (!_om_decoder_next_chunk_position(decoder, &data_read->nextChunk)) {
547 // No next chunk, finish processing the current one and stop
548 break;
549 }
550 } else {
551 data_read->nextChunk.lowerBound += 1;
552 }
553
554 if (data_read->nextChunk.lowerBound >= data_read->indexRange.upperBound) {
555 data_read->nextChunk.lowerBound = 0;
556 data_read->nextChunk.upperBound = 0;
557 break;
558 }
559 }
560
561 // Old files do not compress LUT and data is after LUT
562 // V1 header size
563 const uint64_t om_header_v1_length = sizeof(OmHeaderV1_t);
564 const uint64_t dataStart = om_header_v1_length + number_of_chunks * sizeof(int64_t);
565
566 data_read->offset = startPos + dataStart;
567 data_read->count = endPos - startPos;
568 data_read->chunkIndex.upperBound = chunkIndex + 1;
569 return true;
570 }
571
572 uint8_t* indexDataPtr = (uint8_t*)index_data;
573
574 uint64_t uncompressedLut[LUT_CHUNK_COUNT] = {0};
575
576 // Which LUT chunk is currently loaded into `uncompressedLut`
577 uint64_t lutChunk = chunkIndex / LUT_CHUNK_COUNT;
578
579 const uint64_t lutChunkLength = decoder->lut_chunk_length;
580
581 // Offset byte in LUT relative to the index range
582 const uint64_t lutOffset = data_read->indexRange.lowerBound / LUT_CHUNK_COUNT * lutChunkLength;
583
584 // Uncompress the first LUT index chunk and check the length
585 {
586 const uint64_t thisLutChunkElementCount = om_min((lutChunk + 1) * LUT_CHUNK_COUNT, number_of_chunks+1) - lutChunk * LUT_CHUNK_COUNT;
587 const uint64_t start = lutChunk * lutChunkLength - lutOffset;
588 if (start + lutChunkLength > index_data_size || thisLutChunkElementCount > LUT_CHUNK_COUNT) {
589 (*error) = ERROR_OUT_OF_BOUND_READ;
590 return false;
591 }
592
593 // Decompress LUT chunk
594 p4nddec64(indexDataPtr + start, thisLutChunkElementCount, uncompressedLut);
595 }
596
597 // Index data relative to start index
598 const uint64_t startPos = uncompressedLut[chunkIndex % LUT_CHUNK_COUNT];
599 uint64_t endPos = startPos;
600
601 // Loop to the next chunk until the end is reached
602 while (true) {
603 const uint64_t nextLutChunk = (data_read->nextChunk.lowerBound + 1) / LUT_CHUNK_COUNT;
604
605 // Maybe the next LUT chunk needs to be uncompressed
606 if (nextLutChunk != lutChunk) {
607 const uint64_t nextLutChunkElementCount = om_min((nextLutChunk + 1) * LUT_CHUNK_COUNT, number_of_chunks+1) - nextLutChunk * LUT_CHUNK_COUNT;
608 const uint64_t start = nextLutChunk * lutChunkLength - lutOffset;
609 if (start + lutChunkLength > index_data_size || nextLutChunkElementCount > LUT_CHUNK_COUNT) {
610 (*error) = ERROR_OUT_OF_BOUND_READ;
611 return false;
612 }
613
614 // Decompress LUT chunk
615 p4nddec64(indexDataPtr + start, nextLutChunkElementCount, uncompressedLut);
616 lutChunk = nextLutChunk;
617 }
618
619 const uint64_t dataEndPos = uncompressedLut[(data_read->nextChunk.lowerBound + 1) % LUT_CHUNK_COUNT];
620
621 // Merge and split IO requests, ensuring at least one IO request is sent
622 if (startPos != endPos && (dataEndPos - startPos > decoder->io_size_max || dataEndPos - endPos > decoder->io_size_merge)) {
623 break;
624 }
625 endPos = dataEndPos;
626 chunkIndex = data_read->nextChunk.lowerBound;
627
628 if (chunkIndex + 1 >= data_read->nextChunk.upperBound) {
629 if (!_om_decoder_next_chunk_position(decoder, &data_read->nextChunk)) {
630 // No next chunk, finish processing the current one and stop
631 break;
632 }
633 } else {
634 data_read->nextChunk.lowerBound += 1;
635 }
636
637 if (data_read->nextChunk.lowerBound >= data_read->indexRange.upperBound) {
638 data_read->nextChunk.lowerBound = 0;
639 data_read->nextChunk.upperBound = 0;
640 break;
641 }
642 }
643
644 data_read->offset = (uint64_t)startPos;
645 data_read->count = (uint64_t)endPos - (uint64_t)startPos;
646 data_read->chunkIndex.upperBound = chunkIndex + 1;
647 return true;
648}
649
650// Internal function to decode a single chunk.
651uint64_t _om_decoder_decode_chunk(
652 const OmDecoder_t *decoder,
653 uint64_t chunkIndex,
654 const void *data,
655 uint8_t *into,
656 uint8_t *chunk_buffer
657) {
658 uint64_t rollingMultiply = 1;
659 uint64_t rollingMultiplyChunkLength = 1;
660 uint64_t rollingMultiplyTargetCube = 1;
661
662 int64_t d = 0; // Read coordinate.
663 int64_t q = 0; // Write coordinate.
664 int64_t linearReadCount = 1;
665 bool linearRead = true;
666 int64_t lengthLast = 0;
667 bool no_data = false;
668
669 const uint64_t dimensions_count = decoder->dimensions_count;
670
671 //printf("decode dimcount=%d \n", decoder->dims_count );
672
673 // Count length in chunk and find first buffer offset position.
674 for (uint64_t i_forward = 0; i_forward < dimensions_count; i_forward++) {
675 const uint64_t i = dimensions_count - i_forward - 1;
676 const uint64_t dimension = decoder->dimensions[i];
677 const uint64_t chunk = decoder->chunks[i];
678 const uint64_t read_offset = decoder->read_offset[i];
679 const uint64_t read_count = decoder->read_count[i];
680 const uint64_t cube_offset = decoder->cube_offset == NULL ? 0 : decoder->cube_offset[i];
681 const uint64_t cube_dimension = decoder->cube_dimensions == NULL ? read_count : decoder->cube_dimensions[i];
682
683 const uint64_t nChunksInThisDimension = divide_rounded_up(dimension, chunk);
684 const uint64_t c0 = (chunkIndex / rollingMultiply) % nChunksInThisDimension;
685 const uint64_t chunkGlobal0Start = c0 * chunk;
686 const uint64_t chunkGlobal0End = om_min((c0+1) * chunk, dimension);
687 const uint64_t length0 = chunkGlobal0End - chunkGlobal0Start;
688 const uint64_t clampedGlobal0Start = om_max(chunkGlobal0Start, read_offset);
689 const uint64_t clampedGlobal0End = om_min(chunkGlobal0End, read_offset + read_count);
690 const uint64_t clampedLocal0Start = clampedGlobal0Start - c0 * chunk;
691 const uint64_t lengthRead = clampedGlobal0End - clampedGlobal0Start;
692
693 if (read_offset + read_count <= chunkGlobal0Start || read_offset >= chunkGlobal0End) {
694 no_data = true;
695 }
696
697 if (i == dimensions_count - 1) {
698 lengthLast = length0;
699 }
700
701 const uint64_t d0 = clampedLocal0Start;
702 const uint64_t t0 = chunkGlobal0Start - read_offset + d0;
703 const uint64_t q0 = t0 + cube_offset;
704
705 d += rollingMultiplyChunkLength * d0;
706 q += rollingMultiplyTargetCube * q0;
707
708 if (i == dimensions_count - 1 && !(lengthRead == length0 && read_count == length0 && cube_dimension == length0)) {
709 // if fast dimension and only partially read
710 linearReadCount = lengthRead;
711 linearRead = false;
712 }
713
714 if (linearRead && lengthRead == length0 && read_count == length0 && cube_dimension == length0) {
715 // dimension is read entirely
716 // and can be copied linearly into the output buffer
717 linearReadCount *= length0;
718 } else {
719 // dimension is read partly, cannot merge further reads
720 linearRead = false;
721 }
722
723 rollingMultiply *= nChunksInThisDimension;
724 rollingMultiplyTargetCube *= cube_dimension;
725 rollingMultiplyChunkLength *= length0;
726 }
727
728 const uint64_t lengthInChunk = rollingMultiplyChunkLength;
729
730 const uint64_t uncompressedBytes = om_decode_decompress(
731 decoder->data_type,
732 decoder->compression,
733 data,
734 lengthInChunk,
735 chunk_buffer
736 );
737
738 if (no_data) {
739 return uncompressedBytes;
740 }
741
742 // Perform 2D decoding
743 om_decode_filter(decoder->data_type, decoder->compression, chunk_buffer, lengthInChunk, lengthLast);
744
745 // Copy data from the chunk buffer to the output buffer.
746 while (true) {
747 // Copy values from chunk buffer into output buffer
748 om_decode_copy(
749 decoder->data_type,
750 decoder->compression,
751 linearReadCount,
752 decoder->scale_factor,
753 decoder->add_offset,
754 chunk_buffer + d * decoder->bytes_per_element_compressed,
755 into + q * decoder->bytes_per_element
756 );
757
758 q += linearReadCount - 1;
759 d += linearReadCount - 1;
760
761 rollingMultiply = 1;
762 rollingMultiplyTargetCube = 1;
763 rollingMultiplyChunkLength = 1;
764 linearReadCount = 1;
765 linearRead = true;
766 for (uint64_t i_forward = 0; i_forward < dimensions_count; i_forward++) {
767 const uint64_t i = dimensions_count - i_forward - 1;
768 const uint64_t dimension = decoder->dimensions[i];
769 const uint64_t chunk = decoder->chunks[i];
770 const uint64_t read_offset = decoder->read_offset[i];
771 const uint64_t read_count = decoder->read_count[i];
772 const uint64_t cube_dimension = decoder->cube_dimensions == NULL ? read_count : decoder->cube_dimensions[i];
773
774 //printf("i=%d q=%d d=%d\n", i,q,d);
775 const uint64_t nChunksInThisDimension = divide_rounded_up(dimension, chunk);
776 const uint64_t c0 = (chunkIndex / rollingMultiply) % nChunksInThisDimension;
777 const uint64_t chunkGlobal0Start = c0 * chunk;
778 const uint64_t chunkGlobal0End = om_min((c0+1) * chunk, dimension);
779 const uint64_t length0 = chunkGlobal0End - chunkGlobal0Start;
780 const uint64_t clampedGlobal0Start = om_max(chunkGlobal0Start, read_offset);
781 const uint64_t clampedGlobal0End = om_min(chunkGlobal0End, read_offset + read_count);
782 const uint64_t clampedLocal0End = clampedGlobal0End - chunkGlobal0Start;
783 const uint64_t lengthRead = clampedGlobal0End - clampedGlobal0Start;
784
785 d += rollingMultiplyChunkLength;
786 q += rollingMultiplyTargetCube;
787
788 if ((i == dimensions_count - 1) && !(lengthRead == length0 && read_count == length0 && cube_dimension == length0)) {
789 // if fast dimension and only partially read
790 linearReadCount = lengthRead;
791 linearRead = false;
792 }
793 if (linearRead && (lengthRead == length0) && (read_count == length0) && (cube_dimension == length0)) {
794 // dimension is read entirely
795 // and can be copied linearly into the output buffer
796 linearReadCount *= length0;
797 } else {
798 // dimension is read partly, cannot merge further reads
799 linearRead = false;
800 }
801
802 const uint64_t d0 = (d / rollingMultiplyChunkLength) % length0;
803 if (d0 != clampedLocal0End && d0 != 0) {
804 //printf("break\n");
805 break; // No overflow in this dimension, break
806 }
807
808 d -= lengthRead * rollingMultiplyChunkLength;
809 q -= lengthRead * rollingMultiplyTargetCube;
810
811 rollingMultiply *= nChunksInThisDimension;
812 rollingMultiplyTargetCube *= cube_dimension;
813 rollingMultiplyChunkLength *= length0;
814 //printf("next iter\n");
815 if (i == 0) {
816 return uncompressedBytes; // All chunks have been read. End of iteration
817 }
818 }
819 }
820 return uncompressedBytes;
821}
822
823bool om_decoder_decode_chunks(const OmDecoder_t *decoder, OmRange_t chunk, const void *data, uint64_t data_size, void *into, void *chunkBuffer, OmError_t *error) {
824 uint64_t pos = 0;
825 // printf("chunkIndex.lowerBound %lu %lu\n",chunk.lowerBound,chunk.upperBound);
826 for (uint64_t chunkNum = chunk.lowerBound; chunkNum < chunk.upperBound; ++chunkNum) {
827 // printf("chunkIndex %lu pos=%lu dataCount=%lu \n",chunkNum, pos, data_size);
828 if (pos >= data_size) {
829 (*error) = ERROR_DEFLATED_SIZE_MISMATCH;
830 return false;
831 }
832 if (*error != ERROR_OK) {
833 return false;
834 }
835 uint64_t uncompressedBytes = _om_decoder_decode_chunk(decoder, chunkNum, (const uint8_t *)data + pos, into, chunkBuffer);
836 pos += uncompressedBytes;
837 }
838 // printf("%lu %lu \n", pos, data_size);
839
840 if (pos != data_size) {
841 (*error) = ERROR_DEFLATED_SIZE_MISMATCH;
842 return false;
843 }
844 return pos;
845}