Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / third_party / om-file-format-sys / c / src / om_encoder.c
om_encoder.c428 lines · 16.8 KiBRaw
| 1 | // |
| 2 | // om_encoder.c |
| 3 | // OpenMeteoApi |
| 4 | // |
| 5 | // Created by Patrick Zippenfenig on 29.10.2024. |
| 6 | // |
| 7 | |
| 8 | #include "om_encoder.h" |
| 9 | #include <assert.h> |
| 10 | #include "vp4.h" |
| 11 | #include "fp.h" |
| 12 | #include "delta2d.h" |
| 13 | #include "conf.h" |
| 14 | |
| 15 | #pragma clang diagnostic error "-Wswitch" |
| 16 | |
| 17 | OmError_t om_encoder_init( |
| 18 | OmEncoder_t* encoder, |
| 19 | float scale_factor, |
| 20 | float add_offset, |
| 21 | OmCompression_t compression, |
| 22 | OmDataType_t data_type, |
| 23 | const uint64_t* dimensions, |
| 24 | const uint64_t* chunks, |
| 25 | uint64_t dimension_count |
| 26 | ) { |
| 27 | for (uint64_t i = 0; i < dimension_count; i++) { |
| 28 | if (dimensions[i] == 0) { |
| 29 | return ERROR_INVALID_DIMENSIONS; |
| 30 | } |
| 31 | if (chunks[i] == 0 || chunks[i] > dimensions[i]) { |
| 32 | return ERROR_INVALID_CHUNK_DIMENSIONS; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | encoder->scale_factor = scale_factor; |
| 37 | encoder->add_offset = add_offset; |
| 38 | encoder->dimensions = dimensions; |
| 39 | encoder->chunks = chunks; |
| 40 | encoder->dimension_count = dimension_count; |
| 41 | encoder->data_type = data_type; |
| 42 | encoder->compression = compression; |
| 43 | |
| 44 | OmError_t error = ERROR_OK; |
| 45 | encoder->bytes_per_element = om_get_bytes_per_element(data_type, &error); |
| 46 | encoder->bytes_per_element_compressed = om_get_bytes_per_element_compressed(data_type, compression, &error); |
| 47 | |
| 48 | return error; |
| 49 | } |
| 50 | |
| 51 | ALWAYS_INLINE uint64_t om_encode_compress( |
| 52 | OmDataType_t data_type, |
| 53 | OmCompression_t compression_type, |
| 54 | const void* input, |
| 55 | uint64_t count, |
| 56 | void* output |
| 57 | ) { |
| 58 | uint64_t result = 0; |
| 59 | |
| 60 | switch (compression_type) { |
| 61 | case COMPRESSION_PFOR_DELTA2D_INT16: |
| 62 | case COMPRESSION_PFOR_DELTA2D_INT16_LOGARITHMIC: |
| 63 | // The initializer should have ensured that the data type is float |
| 64 | assert(data_type == DATA_TYPE_FLOAT_ARRAY && "Expecting float array"); |
| 65 | result = p4nzenc128v16((uint16_t*)input, (size_t)count, (unsigned char*)output); |
| 66 | break; |
| 67 | |
| 68 | case COMPRESSION_FPX_XOR2D: |
| 69 | assert(data_type == DATA_TYPE_FLOAT_ARRAY || data_type == DATA_TYPE_DOUBLE_ARRAY && "Expecting float or double array"); |
| 70 | if (data_type == DATA_TYPE_FLOAT_ARRAY) { |
| 71 | result = om_common_compress_fpxenc32((float*)input, (size_t)count, (unsigned char*)output); |
| 72 | } else if (data_type == DATA_TYPE_DOUBLE_ARRAY) { |
| 73 | result = om_common_compress_fpxenc64((double*)input, (size_t)count, (unsigned char*)output); |
| 74 | } |
| 75 | break; |
| 76 | |
| 77 | case COMPRESSION_PFOR_DELTA2D: |
| 78 | switch (data_type) { |
| 79 | case DATA_TYPE_INT8_ARRAY: |
| 80 | result = p4nzenc8((uint8_t*)input, (size_t)count, (unsigned char*)output); |
| 81 | break; |
| 82 | case DATA_TYPE_UINT8_ARRAY: |
| 83 | result = p4ndenc8((uint8_t*)input, (size_t)count, (unsigned char*)output); |
| 84 | break; |
| 85 | case DATA_TYPE_INT16_ARRAY: |
| 86 | result = p4nzenc128v16((uint16_t*)input, (size_t)count, (unsigned char*)output); |
| 87 | break; |
| 88 | case DATA_TYPE_UINT16_ARRAY: |
| 89 | result = p4ndenc128v16((uint16_t*)input, (size_t)count, (unsigned char*)output); |
| 90 | break; |
| 91 | case DATA_TYPE_INT32_ARRAY: |
| 92 | result = p4nzenc128v32((uint32_t*)input, (size_t)count, (unsigned char*)output); |
| 93 | break; |
| 94 | case DATA_TYPE_UINT32_ARRAY: |
| 95 | result = p4ndenc128v32((uint32_t*)input, (size_t)count, (unsigned char*)output); |
| 96 | break; |
| 97 | case DATA_TYPE_INT64_ARRAY: |
| 98 | result = p4nzenc64((uint64_t*)input, (size_t)count, (unsigned char*)output); |
| 99 | break; |
| 100 | case DATA_TYPE_UINT64_ARRAY: |
| 101 | result = p4ndenc64((uint64_t*)input, (size_t)count, (unsigned char*)output); |
| 102 | break; |
| 103 | case DATA_TYPE_FLOAT_ARRAY: |
| 104 | result = p4nzenc128v32((uint32_t*)input, (size_t)count, (unsigned char*)output); |
| 105 | break; |
| 106 | case DATA_TYPE_DOUBLE_ARRAY: |
| 107 | result = p4nzenc64((uint64_t*)input, (size_t)count, (unsigned char*)output); |
| 108 | break; |
| 109 | case DATA_TYPE_NONE: |
| 110 | case DATA_TYPE_STRING: |
| 111 | case DATA_TYPE_STRING_ARRAY: |
| 112 | case DATA_TYPE_INT8: |
| 113 | case DATA_TYPE_UINT8: |
| 114 | case DATA_TYPE_INT16: |
| 115 | case DATA_TYPE_UINT16: |
| 116 | case DATA_TYPE_INT32: |
| 117 | case DATA_TYPE_UINT32: |
| 118 | case DATA_TYPE_INT64: |
| 119 | case DATA_TYPE_UINT64: |
| 120 | case DATA_TYPE_FLOAT: |
| 121 | case DATA_TYPE_DOUBLE: |
| 122 | break; |
| 123 | } |
| 124 | break; |
| 125 | |
| 126 | case COMPRESSION_NONE: |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | return result; |
| 131 | } |
| 132 | |
| 133 | ALWAYS_INLINE void om_encode_filter( |
| 134 | OmDataType_t data_type, |
| 135 | OmCompression_t compression_type, |
| 136 | void* data, |
| 137 | uint64_t length_in_chunk, |
| 138 | uint64_t length_last |
| 139 | ) { |
| 140 | switch (compression_type) { |
| 141 | case COMPRESSION_PFOR_DELTA2D_INT16: |
| 142 | case COMPRESSION_PFOR_DELTA2D_INT16_LOGARITHMIC: |
| 143 | // The initializer should have ensured that the data type is float |
| 144 | assert(data_type == DATA_TYPE_FLOAT_ARRAY && "Expecting float array"); |
| 145 | delta2d_encode16((size_t)(length_in_chunk / length_last), (size_t)length_last, (int16_t*)data); |
| 146 | break; |
| 147 | |
| 148 | case COMPRESSION_FPX_XOR2D: |
| 149 | assert(data_type == DATA_TYPE_FLOAT_ARRAY || data_type == DATA_TYPE_DOUBLE_ARRAY && "Expecting float or double array"); |
| 150 | if (data_type == DATA_TYPE_FLOAT_ARRAY) { |
| 151 | delta2d_encode_xor((size_t)(length_in_chunk / length_last), (size_t)length_last, (float*)data); |
| 152 | } else if (data_type == DATA_TYPE_DOUBLE_ARRAY) { |
| 153 | delta2d_encode_xor_double((size_t)(length_in_chunk / length_last), (size_t)length_last, (double*)data); |
| 154 | } |
| 155 | break; |
| 156 | |
| 157 | case COMPRESSION_PFOR_DELTA2D: |
| 158 | switch (data_type) { |
| 159 | case DATA_TYPE_INT8_ARRAY: |
| 160 | case DATA_TYPE_UINT8_ARRAY: |
| 161 | delta2d_encode8((size_t)(length_in_chunk / length_last), (size_t)length_last, (int8_t*)data); |
| 162 | break; |
| 163 | case DATA_TYPE_INT16_ARRAY: |
| 164 | case DATA_TYPE_UINT16_ARRAY: |
| 165 | delta2d_encode16((size_t)(length_in_chunk / length_last), (size_t)length_last, (int16_t*)data); |
| 166 | break; |
| 167 | case DATA_TYPE_INT32_ARRAY: |
| 168 | case DATA_TYPE_UINT32_ARRAY: |
| 169 | case DATA_TYPE_FLOAT_ARRAY: |
| 170 | delta2d_encode32((size_t)(length_in_chunk / length_last), (size_t)length_last, (int32_t*)data); |
| 171 | break; |
| 172 | case DATA_TYPE_INT64_ARRAY: |
| 173 | case DATA_TYPE_UINT64_ARRAY: |
| 174 | case DATA_TYPE_DOUBLE_ARRAY: |
| 175 | delta2d_encode64((size_t)(length_in_chunk / length_last), (size_t)length_last, (int64_t*)data); |
| 176 | break; |
| 177 | default: |
| 178 | break; |
| 179 | } |
| 180 | break; |
| 181 | case COMPRESSION_NONE: |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | ALWAYS_INLINE void om_encode_copy( |
| 187 | OmDataType_t data_type, |
| 188 | OmCompression_t compression_type, |
| 189 | uint64_t count, |
| 190 | float scale_factor, |
| 191 | float add_offset, |
| 192 | const void* input, |
| 193 | void* output |
| 194 | ) { |
| 195 | switch (compression_type) { |
| 196 | case COMPRESSION_PFOR_DELTA2D_INT16: |
| 197 | assert(data_type == DATA_TYPE_FLOAT_ARRAY && "Expecting float array"); |
| 198 | om_common_copy_float_to_int16(count, scale_factor, add_offset, input, output); |
| 199 | break; |
| 200 | |
| 201 | case COMPRESSION_PFOR_DELTA2D_INT16_LOGARITHMIC: |
| 202 | assert(data_type == DATA_TYPE_FLOAT_ARRAY && "Expecting float array"); |
| 203 | om_common_copy_float_to_int16_log10(count, scale_factor, input, output); |
| 204 | break; |
| 205 | |
| 206 | case COMPRESSION_FPX_XOR2D: |
| 207 | assert(data_type == DATA_TYPE_FLOAT_ARRAY || data_type == DATA_TYPE_DOUBLE_ARRAY && "Expecting float or double array"); |
| 208 | if (data_type == DATA_TYPE_FLOAT_ARRAY) { |
| 209 | om_common_copy32(count, input, output); |
| 210 | } else if (data_type == DATA_TYPE_DOUBLE_ARRAY) { |
| 211 | om_common_copy64(count, input, output); |
| 212 | } |
| 213 | break; |
| 214 | |
| 215 | case COMPRESSION_PFOR_DELTA2D: |
| 216 | switch (data_type) { |
| 217 | case DATA_TYPE_INT8_ARRAY: |
| 218 | case DATA_TYPE_UINT8_ARRAY: |
| 219 | om_common_copy8(count, input, output); |
| 220 | break; |
| 221 | case DATA_TYPE_INT16_ARRAY: |
| 222 | case DATA_TYPE_UINT16_ARRAY: |
| 223 | om_common_copy16(count, input, output); |
| 224 | break; |
| 225 | case DATA_TYPE_INT32_ARRAY: |
| 226 | case DATA_TYPE_UINT32_ARRAY: |
| 227 | om_common_copy32(count, input, output); |
| 228 | break; |
| 229 | case DATA_TYPE_FLOAT_ARRAY: |
| 230 | om_common_copy_float_to_int32(count, scale_factor, add_offset, input, output); |
| 231 | break; |
| 232 | case DATA_TYPE_INT64_ARRAY: |
| 233 | case DATA_TYPE_UINT64_ARRAY: |
| 234 | om_common_copy64(count, input, output); |
| 235 | break; |
| 236 | case DATA_TYPE_DOUBLE_ARRAY: |
| 237 | om_common_copy_double_to_int64(count, scale_factor, add_offset, input, output); |
| 238 | break; |
| 239 | default: |
| 240 | break; |
| 241 | } |
| 242 | break; |
| 243 | |
| 244 | case COMPRESSION_NONE: |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | uint64_t om_encoder_count_chunks(const OmEncoder_t* encoder) { |
| 250 | uint64_t n = 1; |
| 251 | for (uint64_t i = 0; i < encoder->dimension_count; i++) { |
| 252 | n *= divide_rounded_up(encoder->dimensions[i], encoder->chunks[i]); |
| 253 | } |
| 254 | return n; |
| 255 | } |
| 256 | |
| 257 | uint64_t om_encoder_count_chunks_in_array(const OmEncoder_t* encoder, const uint64_t* array_count) { |
| 258 | uint64_t numberOfChunksInArray = 1; |
| 259 | for (uint64_t i = 0; i < encoder->dimension_count; i++) { |
| 260 | numberOfChunksInArray *= divide_rounded_up(array_count[i], encoder->chunks[i]); |
| 261 | } |
| 262 | return numberOfChunksInArray; |
| 263 | } |
| 264 | |
| 265 | uint64_t om_encoder_chunk_buffer_size(const OmEncoder_t* encoder) { |
| 266 | uint64_t chunkLength = 1; |
| 267 | for (uint64_t i = 0; i < encoder->dimension_count; i++) { |
| 268 | chunkLength *= encoder->chunks[i]; |
| 269 | } |
| 270 | return chunkLength * encoder->bytes_per_element_compressed; |
| 271 | } |
| 272 | |
| 273 | uint64_t om_encoder_compressed_chunk_buffer_size(const OmEncoder_t* encoder) { |
| 274 | uint64_t chunkLength = 1; |
| 275 | for (uint64_t i = 0; i < encoder->dimension_count; i++) { |
| 276 | chunkLength *= encoder->chunks[i]; |
| 277 | } |
| 278 | // P4NENC256_BOUND. Compressor may write 32 integers more |
| 279 | return (chunkLength + 255) /256 + (chunkLength + 32) * encoder->bytes_per_element_compressed; |
| 280 | } |
| 281 | |
| 282 | uint64_t om_encoder_lut_buffer_size(const uint64_t* lookUpTable, uint64_t lookUpTableCount) { |
| 283 | uint64_t buffer[LUT_CHUNK_COUNT+32] = {0}; |
| 284 | const uint64_t nLutChunks = divide_rounded_up(lookUpTableCount, LUT_CHUNK_COUNT); |
| 285 | uint64_t maxLength = 0; |
| 286 | for (uint64_t i = 0; i < nLutChunks; i++) { |
| 287 | const uint64_t rangeStart = i * LUT_CHUNK_COUNT; |
| 288 | const uint64_t rangeEnd = om_min(rangeStart + LUT_CHUNK_COUNT, lookUpTableCount); |
| 289 | const uint64_t len = p4ndenc64((uint64_t*)&lookUpTable[rangeStart], rangeEnd - rangeStart, (unsigned char *)buffer); |
| 290 | if (len > maxLength) maxLength = len; |
| 291 | } |
| 292 | // Compression function can write 32 integers more |
| 293 | return maxLength * nLutChunks + 32 * sizeof(uint64_t); |
| 294 | } |
| 295 | |
| 296 | uint64_t om_encoder_compress_lut(const uint64_t* lookUpTable, uint64_t lookUpTableCount, uint8_t* out, uint64_t compressed_lut_buffer_size) { |
| 297 | const uint64_t nLutChunks = divide_rounded_up(lookUpTableCount, LUT_CHUNK_COUNT); |
| 298 | const uint64_t lutSize = compressed_lut_buffer_size - 32 * sizeof(uint64_t); |
| 299 | const uint64_t lutChunkLength = lutSize / nLutChunks; |
| 300 | |
| 301 | for (uint64_t i = 0; i < nLutChunks; i++) { |
| 302 | const uint64_t rangeStart = i * LUT_CHUNK_COUNT; |
| 303 | const uint64_t rangeEnd = om_min(rangeStart + LUT_CHUNK_COUNT, lookUpTableCount); |
| 304 | const uint64_t len = p4ndenc64((uint64_t*)&lookUpTable[rangeStart], rangeEnd - rangeStart, &out[i * lutChunkLength]); |
| 305 | for (uint64_t j = i * lutChunkLength + len; j < (i+1) * lutChunkLength; j++) { |
| 306 | out[j] = 0; // fill remaining space with 0 |
| 307 | } |
| 308 | } |
| 309 | return lutSize; |
| 310 | } |
| 311 | |
| 312 | uint64_t om_encoder_compress_chunk( |
| 313 | const OmEncoder_t* encoder, |
| 314 | const void* array, |
| 315 | const uint64_t* arrayDimensions, |
| 316 | const uint64_t* arrayOffset, |
| 317 | const uint64_t* arrayCount, |
| 318 | uint64_t chunkIndex, |
| 319 | uint64_t chunkIndexOffsetInThisArray, |
| 320 | uint8_t* out, |
| 321 | uint8_t* chunkBuffer |
| 322 | ) { |
| 323 | |
| 324 | const uint64_t dimension_count = encoder->dimension_count; |
| 325 | // The total size of `arrayDimensions`. Only used to check for out of bound reads |
| 326 | uint64_t arrayTotalCount = 1; |
| 327 | for (uint64_t i = 0; i < dimension_count; i++) { |
| 328 | arrayTotalCount *= arrayDimensions[i]; |
| 329 | } |
| 330 | |
| 331 | uint64_t rollingMultiply = 1; |
| 332 | uint64_t rollingMultiplyChunkLength = 1; |
| 333 | uint64_t rollingMultiplyTargetCube = 1; |
| 334 | uint64_t readCoordinate = 0; |
| 335 | uint64_t writeCoordinate = 0; |
| 336 | uint64_t linearReadCount = 1; |
| 337 | bool linearRead = true; |
| 338 | uint64_t lengthLast = 0; |
| 339 | |
| 340 | for (uint64_t i_forward = 0; i_forward < dimension_count; i_forward++) { |
| 341 | const uint64_t i = dimension_count - i_forward - 1; |
| 342 | const uint64_t dimension = encoder->dimensions[i]; |
| 343 | const uint64_t chunk = encoder->chunks[i]; |
| 344 | |
| 345 | const uint64_t nChunksInThisDimension = divide_rounded_up(dimension, chunk); |
| 346 | const uint64_t c0 = (chunkIndex / rollingMultiply) % nChunksInThisDimension; |
| 347 | const uint64_t c0Offset = (chunkIndexOffsetInThisArray / rollingMultiply) % nChunksInThisDimension; |
| 348 | const uint64_t length0 = om_min((c0 + 1) * chunk, dimension) - c0 * chunk; |
| 349 | |
| 350 | if (i == dimension_count - 1) { |
| 351 | lengthLast = length0; |
| 352 | } |
| 353 | |
| 354 | readCoordinate += rollingMultiplyTargetCube * (c0Offset * encoder->chunks[i] + arrayOffset[i]); |
| 355 | assert(length0 <= arrayCount[i]); |
| 356 | assert(length0 <= arrayDimensions[i]); |
| 357 | |
| 358 | if (i == dimension_count - 1 && !(arrayCount[i] == length0 && arrayDimensions[i] == length0)) { |
| 359 | linearReadCount = length0; |
| 360 | linearRead = false; |
| 361 | } |
| 362 | if (linearRead && arrayCount[i] == length0 && arrayDimensions[i] == length0) { |
| 363 | linearReadCount *= length0; |
| 364 | } else { |
| 365 | linearRead = false; |
| 366 | } |
| 367 | |
| 368 | rollingMultiply *= nChunksInThisDimension; |
| 369 | rollingMultiplyTargetCube *= arrayDimensions[i]; |
| 370 | rollingMultiplyChunkLength *= length0; |
| 371 | } |
| 372 | |
| 373 | const uint64_t lengthInChunk = rollingMultiplyChunkLength; |
| 374 | |
| 375 | while (true) { |
| 376 | assert(readCoordinate + linearReadCount <= arrayTotalCount); |
| 377 | assert(writeCoordinate + linearReadCount <= lengthInChunk); |
| 378 | |
| 379 | om_encode_copy( |
| 380 | encoder->data_type, |
| 381 | encoder->compression, |
| 382 | linearReadCount, |
| 383 | encoder->scale_factor, |
| 384 | encoder->add_offset, |
| 385 | (const uint8_t*)array + encoder->bytes_per_element * readCoordinate, |
| 386 | chunkBuffer + encoder->bytes_per_element_compressed * writeCoordinate |
| 387 | ); |
| 388 | |
| 389 | readCoordinate += linearReadCount - 1; |
| 390 | writeCoordinate += linearReadCount - 1; |
| 391 | writeCoordinate += 1; |
| 392 | |
| 393 | rollingMultiplyTargetCube = 1; |
| 394 | linearRead = true; |
| 395 | linearReadCount = 1; |
| 396 | |
| 397 | for (uint64_t i_forward = 0; i_forward < dimension_count; i_forward++) { |
| 398 | uint64_t i = dimension_count - i_forward - 1; |
| 399 | const uint64_t chunk = encoder->chunks[i]; |
| 400 | |
| 401 | const uint64_t qPos = ((readCoordinate / rollingMultiplyTargetCube) % arrayDimensions[i] - arrayOffset[i]) / chunk; |
| 402 | const uint64_t length0 = om_min((qPos + 1) * chunk, arrayCount[i]) - qPos * chunk; |
| 403 | readCoordinate += rollingMultiplyTargetCube; |
| 404 | |
| 405 | if (i == dimension_count - 1 && !(arrayCount[i] == length0 && arrayDimensions[i] == length0)) { |
| 406 | linearReadCount = length0; |
| 407 | linearRead = false; |
| 408 | } |
| 409 | if (linearRead && arrayCount[i] == length0 && arrayDimensions[i] == length0) { |
| 410 | linearReadCount *= length0; |
| 411 | } else { |
| 412 | linearRead = false; |
| 413 | } |
| 414 | const uint64_t q0 = ((readCoordinate / rollingMultiplyTargetCube) % arrayDimensions[i] - arrayOffset[i]) % encoder->chunks[i]; |
| 415 | if (q0 != 0 && q0 != length0) { |
| 416 | break; |
| 417 | } |
| 418 | readCoordinate -= length0 * rollingMultiplyTargetCube; |
| 419 | rollingMultiplyTargetCube *= arrayDimensions[i]; |
| 420 | |
| 421 | if (i == 0) { |
| 422 | om_encode_filter(encoder->data_type, encoder->compression, chunkBuffer, lengthInChunk, lengthLast); |
| 423 | uint64_t compressed_length = om_encode_compress(encoder->data_type, encoder->compression, chunkBuffer, lengthInChunk, out); |
| 424 | return compressed_length; |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | } |