| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#pragma once |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#ifndef BASISD_SUPPORT_KTX2 |
| 22 |
#define BASISD_SUPPORT_KTX2 1 |
| 23 |
#endif |
| 24 |
|
| 25 |
|
| 26 |
#ifndef BASISD_SUPPORT_KTX2_ZSTD |
| 27 |
#define BASISD_SUPPORT_KTX2_ZSTD 1 |
| 28 |
#endif |
| 29 |
|
| 30 |
|
| 31 |
#ifndef BASISU_FORCE_DEVEL_MESSAGES |
| 32 |
|
| 33 |
#define BASISU_FORCE_DEVEL_MESSAGES 0 |
| 34 |
#endif |
| 35 |
|
| 36 |
#include "basisu_transcoder_internal.h" |
| 37 |
#include "basisu_transcoder_uastc.h" |
| 38 |
#include "basisu_file_headers.h" |
| 39 |
|
| 40 |
namespace basist |
| 41 |
{ |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
enum class transcoder_texture_format |
| 51 |
{ |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
cTFETC1_RGB = 0, |
| 56 |
cTFETC2_RGBA = 1, |
| 57 |
|
| 58 |
|
| 59 |
cTFBC1_RGB = 2, |
| 60 |
cTFBC3_RGBA = 3, |
| 61 |
cTFBC4_R = 4, |
| 62 |
cTFBC5_RG = 5, |
| 63 |
cTFBC7_RGBA = 6, |
| 64 |
|
| 65 |
|
| 66 |
cTFPVRTC1_4_RGB = 8, |
| 67 |
cTFPVRTC1_4_RGBA = 9, |
| 68 |
|
| 69 |
|
| 70 |
cTFASTC_4x4_RGBA = 10, |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
cTFATC_RGB = 11, |
| 75 |
cTFATC_RGBA = 12, |
| 76 |
|
| 77 |
|
| 78 |
cTFFXT1_RGB = 17, |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
cTFPVRTC2_4_RGB = 18, |
| 83 |
cTFPVRTC2_4_RGBA = 19, |
| 84 |
|
| 85 |
cTFETC2_EAC_R11 = 20, |
| 86 |
cTFETC2_EAC_RG11 = 21, |
| 87 |
|
| 88 |
cTFBC6H = 22, |
| 89 |
cTFASTC_HDR_4x4_RGBA = 23, |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
cTFRGBA32 = 13, |
| 94 |
cTFRGB565 = 14, |
| 95 |
cTFBGR565 = 15, |
| 96 |
cTFRGBA4444 = 16, |
| 97 |
|
| 98 |
|
| 99 |
cTFRGB_HALF = 24, |
| 100 |
cTFRGBA_HALF = 25, |
| 101 |
cTFRGB_9E5 = 26, |
| 102 |
|
| 103 |
cTFASTC_HDR_6x6_RGBA = 27, |
| 104 |
|
| 105 |
cTFTotalTextureFormats = 28, |
| 106 |
|
| 107 |
|
| 108 |
cTFETC1 = cTFETC1_RGB, |
| 109 |
cTFETC2 = cTFETC2_RGBA, |
| 110 |
cTFBC1 = cTFBC1_RGB, |
| 111 |
cTFBC3 = cTFBC3_RGBA, |
| 112 |
cTFBC4 = cTFBC4_R, |
| 113 |
cTFBC5 = cTFBC5_RG, |
| 114 |
|
| 115 |
|
| 116 |
cTFBC7_M6_RGB = cTFBC7_RGBA, |
| 117 |
cTFBC7_M5_RGBA = cTFBC7_RGBA, |
| 118 |
cTFBC7_M6_OPAQUE_ONLY = cTFBC7_RGBA, |
| 119 |
cTFBC7_M5 = cTFBC7_RGBA, |
| 120 |
cTFBC7_ALT = 7, |
| 121 |
|
| 122 |
cTFASTC_4x4 = cTFASTC_4x4_RGBA, |
| 123 |
|
| 124 |
cTFATC_RGBA_INTERPOLATED_ALPHA = cTFATC_RGBA, |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
uint32_t basis_get_bytes_per_block_or_pixel(transcoder_texture_format fmt); |
| 130 |
|
| 131 |
|
| 132 |
const char* basis_get_format_name(transcoder_texture_format fmt); |
| 133 |
|
| 134 |
|
| 135 |
const char* basis_get_block_format_name(block_format fmt); |
| 136 |
|
| 137 |
|
| 138 |
bool basis_transcoder_format_has_alpha(transcoder_texture_format fmt); |
| 139 |
|
| 140 |
|
| 141 |
bool basis_transcoder_format_is_hdr(transcoder_texture_format fmt); |
| 142 |
|
| 143 |
|
| 144 |
inline bool basis_transcoder_format_is_ldr(transcoder_texture_format fmt) { return !basis_transcoder_format_is_hdr(fmt); } |
| 145 |
|
| 146 |
|
| 147 |
basisu::texture_format basis_get_basisu_texture_format(transcoder_texture_format fmt); |
| 148 |
|
| 149 |
|
| 150 |
const char* basis_get_texture_type_name(basis_texture_type tex_type); |
| 151 |
|
| 152 |
|
| 153 |
bool basis_transcoder_format_is_uncompressed(transcoder_texture_format tex_type); |
| 154 |
|
| 155 |
|
| 156 |
uint32_t basis_get_uncompressed_bytes_per_pixel(transcoder_texture_format fmt); |
| 157 |
|
| 158 |
|
| 159 |
uint32_t basis_get_block_width(transcoder_texture_format tex_type); |
| 160 |
|
| 161 |
|
| 162 |
uint32_t basis_get_block_height(transcoder_texture_format tex_type); |
| 163 |
|
| 164 |
|
| 165 |
bool basis_is_format_supported(transcoder_texture_format tex_type, basis_tex_format fmt = basis_tex_format::cETC1S); |
| 166 |
|
| 167 |
|
| 168 |
uint32_t basis_tex_format_get_block_width(basis_tex_format fmt); |
| 169 |
uint32_t basis_tex_format_get_block_height(basis_tex_format fmt); |
| 170 |
|
| 171 |
bool basis_tex_format_is_hdr(basis_tex_format fmt); |
| 172 |
inline bool basis_tex_format_is_ldr(basis_tex_format fmt) { return !basis_tex_format_is_hdr(fmt); } |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
bool basis_validate_output_buffer_size(transcoder_texture_format target_format, |
| 177 |
uint32_t output_blocks_buf_size_in_blocks_or_pixels, |
| 178 |
uint32_t orig_width, uint32_t orig_height, |
| 179 |
uint32_t output_row_pitch_in_blocks_or_pixels, |
| 180 |
uint32_t output_rows_in_pixels); |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
uint32_t basis_compute_transcoded_image_size_in_bytes(transcoder_texture_format target_format, uint32_t orig_width, uint32_t orig_height); |
| 186 |
|
| 187 |
class basisu_transcoder; |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
struct basisu_transcoder_state |
| 192 |
{ |
| 193 |
struct block_preds |
| 194 |
{ |
| 195 |
uint16_t m_endpoint_index; |
| 196 |
uint8_t m_pred_bits; |
| 197 |
}; |
| 198 |
|
| 199 |
basisu::vector<block_preds> m_block_endpoint_preds[2]; |
| 200 |
|
| 201 |
enum { cMaxPrevFrameLevels = 16 }; |
| 202 |
basisu::vector<uint32_t> m_prev_frame_indices[2][cMaxPrevFrameLevels]; |
| 203 |
|
| 204 |
void clear() |
| 205 |
{ |
| 206 |
for (uint32_t i = 0; i < 2; i++) |
| 207 |
{ |
| 208 |
m_block_endpoint_preds[i].clear(); |
| 209 |
|
| 210 |
for (uint32_t j = 0; j < cMaxPrevFrameLevels; j++) |
| 211 |
m_prev_frame_indices[i][j].clear(); |
| 212 |
} |
| 213 |
} |
| 214 |
}; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
class basisu_lowlevel_etc1s_transcoder |
| 220 |
{ |
| 221 |
friend class basisu_transcoder; |
| 222 |
|
| 223 |
public: |
| 224 |
basisu_lowlevel_etc1s_transcoder(); |
| 225 |
|
| 226 |
void set_global_codebooks(const basisu_lowlevel_etc1s_transcoder* pGlobal_codebook) { m_pGlobal_codebook = pGlobal_codebook; } |
| 227 |
const basisu_lowlevel_etc1s_transcoder* get_global_codebooks() const { return m_pGlobal_codebook; } |
| 228 |
|
| 229 |
bool decode_palettes( |
| 230 |
uint32_t num_endpoints, const uint8_t* pEndpoints_data, uint32_t endpoints_data_size, |
| 231 |
uint32_t num_selectors, const uint8_t* pSelectors_data, uint32_t selectors_data_size); |
| 232 |
|
| 233 |
bool decode_tables(const uint8_t* pTable_data, uint32_t table_data_size); |
| 234 |
|
| 235 |
bool transcode_slice(void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, const uint8_t* pImage_data, uint32_t image_data_size, block_format fmt, |
| 236 |
uint32_t output_block_or_pixel_stride_in_bytes, bool bc1_allow_threecolor_blocks, const bool is_video, const bool is_alpha_slice, const uint32_t level_index, const uint32_t orig_width, const uint32_t orig_height, uint32_t output_row_pitch_in_blocks_or_pixels = 0, |
| 237 |
basisu_transcoder_state* pState = nullptr, bool astc_transcode_alpha = false, void* pAlpha_blocks = nullptr, uint32_t output_rows_in_pixels = 0, uint32_t decode_flags = 0); |
| 238 |
|
| 239 |
bool transcode_slice(void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, const uint8_t* pImage_data, uint32_t image_data_size, block_format fmt, |
| 240 |
uint32_t output_block_or_pixel_stride_in_bytes, bool bc1_allow_threecolor_blocks, const basis_file_header& header, const basis_slice_desc& slice_desc, uint32_t output_row_pitch_in_blocks_or_pixels = 0, |
| 241 |
basisu_transcoder_state* pState = nullptr, bool astc_transcode_alpha = false, void* pAlpha_blocks = nullptr, uint32_t output_rows_in_pixels = 0, uint32_t decode_flags = 0) |
| 242 |
{ |
| 243 |
return transcode_slice(pDst_blocks, num_blocks_x, num_blocks_y, pImage_data, image_data_size, fmt, output_block_or_pixel_stride_in_bytes, bc1_allow_threecolor_blocks, |
| 244 |
header.m_tex_type == cBASISTexTypeVideoFrames, (slice_desc.m_flags & cSliceDescFlagsHasAlpha) != 0, slice_desc.m_level_index, |
| 245 |
slice_desc.m_orig_width, slice_desc.m_orig_height, output_row_pitch_in_blocks_or_pixels, pState, |
| 246 |
astc_transcode_alpha, |
| 247 |
pAlpha_blocks, |
| 248 |
output_rows_in_pixels, decode_flags); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
bool transcode_image( |
| 253 |
transcoder_texture_format target_format, |
| 254 |
void* pOutput_blocks, uint32_t output_blocks_buf_size_in_blocks_or_pixels, |
| 255 |
const uint8_t* pCompressed_data, uint32_t compressed_data_length, |
| 256 |
uint32_t num_blocks_x, uint32_t num_blocks_y, uint32_t orig_width, uint32_t orig_height, uint32_t level_index, |
| 257 |
uint64_t rgb_offset, uint32_t rgb_length, uint64_t alpha_offset, uint32_t alpha_length, |
| 258 |
uint32_t decode_flags = 0, |
| 259 |
bool basis_file_has_alpha_slices = false, |
| 260 |
bool is_video = false, |
| 261 |
uint32_t output_row_pitch_in_blocks_or_pixels = 0, |
| 262 |
basisu_transcoder_state* pState = nullptr, |
| 263 |
uint32_t output_rows_in_pixels = 0); |
| 264 |
|
| 265 |
void clear() |
| 266 |
{ |
| 267 |
m_local_endpoints.clear(); |
| 268 |
m_local_selectors.clear(); |
| 269 |
m_endpoint_pred_model.clear(); |
| 270 |
m_delta_endpoint_model.clear(); |
| 271 |
m_selector_model.clear(); |
| 272 |
m_selector_history_buf_rle_model.clear(); |
| 273 |
m_selector_history_buf_size = 0; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
typedef basisu::vector<endpoint> endpoint_vec; |
| 278 |
const endpoint_vec& get_endpoints() const { return m_local_endpoints; } |
| 279 |
|
| 280 |
typedef basisu::vector<selector> selector_vec; |
| 281 |
const selector_vec& get_selectors() const { return m_local_selectors; } |
| 282 |
|
| 283 |
private: |
| 284 |
const basisu_lowlevel_etc1s_transcoder* m_pGlobal_codebook; |
| 285 |
|
| 286 |
endpoint_vec m_local_endpoints; |
| 287 |
selector_vec m_local_selectors; |
| 288 |
|
| 289 |
huffman_decoding_table m_endpoint_pred_model, m_delta_endpoint_model, m_selector_model, m_selector_history_buf_rle_model; |
| 290 |
|
| 291 |
uint32_t m_selector_history_buf_size; |
| 292 |
|
| 293 |
basisu_transcoder_state m_def_state; |
| 294 |
}; |
| 295 |
|
| 296 |
enum basisu_decode_flags |
| 297 |
{ |
| 298 |
|
| 299 |
cDecodeFlagsPVRTCDecodeToNextPow2 = 2, |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
cDecodeFlagsTranscodeAlphaDataToOpaqueFormats = 4, |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
cDecodeFlagsBC1ForbidThreeColorBlocks = 8, |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
cDecodeFlagsOutputHasAlphaIndices = 16, |
| 312 |
|
| 313 |
cDecodeFlagsHighQuality = 32, |
| 314 |
|
| 315 |
cDecodeFlagsNoETC1SChromaFiltering = 64 |
| 316 |
}; |
| 317 |
|
| 318 |
|
| 319 |
class basisu_lowlevel_uastc_ldr_4x4_transcoder |
| 320 |
{ |
| 321 |
friend class basisu_transcoder; |
| 322 |
|
| 323 |
public: |
| 324 |
basisu_lowlevel_uastc_ldr_4x4_transcoder(); |
| 325 |
|
| 326 |
bool transcode_slice(void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, const uint8_t* pImage_data, uint32_t image_data_size, block_format fmt, |
| 327 |
uint32_t output_block_or_pixel_stride_in_bytes, bool bc1_allow_threecolor_blocks, bool has_alpha, const uint32_t orig_width, const uint32_t orig_height, uint32_t output_row_pitch_in_blocks_or_pixels = 0, |
| 328 |
basisu_transcoder_state* pState = nullptr, uint32_t output_rows_in_pixels = 0, int channel0 = -1, int channel1 = -1, uint32_t decode_flags = 0); |
| 329 |
|
| 330 |
bool transcode_slice(void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, const uint8_t* pImage_data, uint32_t image_data_size, block_format fmt, |
| 331 |
uint32_t output_block_or_pixel_stride_in_bytes, bool bc1_allow_threecolor_blocks, const basis_file_header& header, const basis_slice_desc& slice_desc, uint32_t output_row_pitch_in_blocks_or_pixels = 0, |
| 332 |
basisu_transcoder_state* pState = nullptr, uint32_t output_rows_in_pixels = 0, int channel0 = -1, int channel1 = -1, uint32_t decode_flags = 0) |
| 333 |
{ |
| 334 |
return transcode_slice(pDst_blocks, num_blocks_x, num_blocks_y, pImage_data, image_data_size, fmt, |
| 335 |
output_block_or_pixel_stride_in_bytes, bc1_allow_threecolor_blocks, (header.m_flags & cBASISHeaderFlagHasAlphaSlices) != 0, slice_desc.m_orig_width, slice_desc.m_orig_height, output_row_pitch_in_blocks_or_pixels, |
| 336 |
pState, output_rows_in_pixels, channel0, channel1, decode_flags); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
bool transcode_image( |
| 341 |
transcoder_texture_format target_format, |
| 342 |
void* pOutput_blocks, uint32_t output_blocks_buf_size_in_blocks_or_pixels, |
| 343 |
const uint8_t* pCompressed_data, uint32_t compressed_data_length, |
| 344 |
uint32_t num_blocks_x, uint32_t num_blocks_y, uint32_t orig_width, uint32_t orig_height, uint32_t level_index, |
| 345 |
uint64_t slice_offset, uint32_t slice_length, |
| 346 |
uint32_t decode_flags = 0, |
| 347 |
bool has_alpha = false, |
| 348 |
bool is_video = false, |
| 349 |
uint32_t output_row_pitch_in_blocks_or_pixels = 0, |
| 350 |
basisu_transcoder_state* pState = nullptr, |
| 351 |
uint32_t output_rows_in_pixels = 0, |
| 352 |
int channel0 = -1, int channel1 = -1); |
| 353 |
}; |
| 354 |
|
| 355 |
|
| 356 |
class basisu_lowlevel_uastc_hdr_4x4_transcoder |
| 357 |
{ |
| 358 |
friend class basisu_transcoder; |
| 359 |
|
| 360 |
public: |
| 361 |
basisu_lowlevel_uastc_hdr_4x4_transcoder(); |
| 362 |
|
| 363 |
bool transcode_slice(void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, const uint8_t* pImage_data, uint32_t image_data_size, block_format fmt, |
| 364 |
uint32_t output_block_or_pixel_stride_in_bytes, bool bc1_allow_threecolor_blocks, bool has_alpha, const uint32_t orig_width, const uint32_t orig_height, uint32_t output_row_pitch_in_blocks_or_pixels = 0, |
| 365 |
basisu_transcoder_state* pState = nullptr, uint32_t output_rows_in_pixels = 0, int channel0 = -1, int channel1 = -1, uint32_t decode_flags = 0); |
| 366 |
|
| 367 |
bool transcode_slice(void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, const uint8_t* pImage_data, uint32_t image_data_size, block_format fmt, |
| 368 |
uint32_t output_block_or_pixel_stride_in_bytes, bool bc1_allow_threecolor_blocks, const basis_file_header& header, const basis_slice_desc& slice_desc, uint32_t output_row_pitch_in_blocks_or_pixels = 0, |
| 369 |
basisu_transcoder_state* pState = nullptr, uint32_t output_rows_in_pixels = 0, int channel0 = -1, int channel1 = -1, uint32_t decode_flags = 0) |
| 370 |
{ |
| 371 |
return transcode_slice(pDst_blocks, num_blocks_x, num_blocks_y, pImage_data, image_data_size, fmt, |
| 372 |
output_block_or_pixel_stride_in_bytes, bc1_allow_threecolor_blocks, (header.m_flags & cBASISHeaderFlagHasAlphaSlices) != 0, slice_desc.m_orig_width, slice_desc.m_orig_height, output_row_pitch_in_blocks_or_pixels, |
| 373 |
pState, output_rows_in_pixels, channel0, channel1, decode_flags); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
bool transcode_image( |
| 378 |
transcoder_texture_format target_format, |
| 379 |
void* pOutput_blocks, uint32_t output_blocks_buf_size_in_blocks_or_pixels, |
| 380 |
const uint8_t* pCompressed_data, uint32_t compressed_data_length, |
| 381 |
uint32_t num_blocks_x, uint32_t num_blocks_y, uint32_t orig_width, uint32_t orig_height, uint32_t level_index, |
| 382 |
uint64_t slice_offset, uint32_t slice_length, |
| 383 |
uint32_t decode_flags = 0, |
| 384 |
bool has_alpha = false, |
| 385 |
bool is_video = false, |
| 386 |
uint32_t output_row_pitch_in_blocks_or_pixels = 0, |
| 387 |
basisu_transcoder_state* pState = nullptr, |
| 388 |
uint32_t output_rows_in_pixels = 0, |
| 389 |
int channel0 = -1, int channel1 = -1); |
| 390 |
}; |
| 391 |
|
| 392 |
|
| 393 |
class basisu_lowlevel_astc_hdr_6x6_transcoder |
| 394 |
{ |
| 395 |
friend class basisu_transcoder; |
| 396 |
|
| 397 |
public: |
| 398 |
basisu_lowlevel_astc_hdr_6x6_transcoder(); |
| 399 |
|
| 400 |
bool transcode_slice(void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, const uint8_t* pImage_data, uint32_t image_data_size, block_format fmt, |
| 401 |
uint32_t output_block_or_pixel_stride_in_bytes, bool bc1_allow_threecolor_blocks, bool has_alpha, const uint32_t orig_width, const uint32_t orig_height, uint32_t output_row_pitch_in_blocks_or_pixels = 0, |
| 402 |
basisu_transcoder_state* pState = nullptr, uint32_t output_rows_in_pixels = 0, int channel0 = -1, int channel1 = -1, uint32_t decode_flags = 0); |
| 403 |
|
| 404 |
bool transcode_slice(void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, const uint8_t* pImage_data, uint32_t image_data_size, block_format fmt, |
| 405 |
uint32_t output_block_or_pixel_stride_in_bytes, bool bc1_allow_threecolor_blocks, const basis_file_header& header, const basis_slice_desc& slice_desc, uint32_t output_row_pitch_in_blocks_or_pixels = 0, |
| 406 |
basisu_transcoder_state* pState = nullptr, uint32_t output_rows_in_pixels = 0, int channel0 = -1, int channel1 = -1, uint32_t decode_flags = 0) |
| 407 |
{ |
| 408 |
return transcode_slice(pDst_blocks, num_blocks_x, num_blocks_y, pImage_data, image_data_size, fmt, |
| 409 |
output_block_or_pixel_stride_in_bytes, bc1_allow_threecolor_blocks, (header.m_flags & cBASISHeaderFlagHasAlphaSlices) != 0, slice_desc.m_orig_width, slice_desc.m_orig_height, output_row_pitch_in_blocks_or_pixels, |
| 410 |
pState, output_rows_in_pixels, channel0, channel1, decode_flags); |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
bool transcode_image( |
| 415 |
transcoder_texture_format target_format, |
| 416 |
void* pOutput_blocks, uint32_t output_blocks_buf_size_in_blocks_or_pixels, |
| 417 |
const uint8_t* pCompressed_data, uint32_t compressed_data_length, |
| 418 |
uint32_t num_blocks_x, uint32_t num_blocks_y, uint32_t orig_width, uint32_t orig_height, uint32_t level_index, |
| 419 |
uint64_t slice_offset, uint32_t slice_length, |
| 420 |
uint32_t decode_flags = 0, |
| 421 |
bool has_alpha = false, |
| 422 |
bool is_video = false, |
| 423 |
uint32_t output_row_pitch_in_blocks_or_pixels = 0, |
| 424 |
basisu_transcoder_state* pState = nullptr, |
| 425 |
uint32_t output_rows_in_pixels = 0, |
| 426 |
int channel0 = -1, int channel1 = -1); |
| 427 |
}; |
| 428 |
|
| 429 |
|
| 430 |
class basisu_lowlevel_astc_hdr_6x6_intermediate_transcoder |
| 431 |
{ |
| 432 |
friend class basisu_transcoder; |
| 433 |
|
| 434 |
public: |
| 435 |
basisu_lowlevel_astc_hdr_6x6_intermediate_transcoder(); |
| 436 |
|
| 437 |
bool transcode_slice(void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, const uint8_t* pImage_data, uint32_t image_data_size, block_format fmt, |
| 438 |
uint32_t output_block_or_pixel_stride_in_bytes, bool bc1_allow_threecolor_blocks, bool has_alpha, const uint32_t orig_width, const uint32_t orig_height, uint32_t output_row_pitch_in_blocks_or_pixels = 0, |
| 439 |
basisu_transcoder_state* pState = nullptr, uint32_t output_rows_in_pixels = 0, int channel0 = -1, int channel1 = -1, uint32_t decode_flags = 0); |
| 440 |
|
| 441 |
bool transcode_slice(void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, const uint8_t* pImage_data, uint32_t image_data_size, block_format fmt, |
| 442 |
uint32_t output_block_or_pixel_stride_in_bytes, bool bc1_allow_threecolor_blocks, const basis_file_header& header, const basis_slice_desc& slice_desc, uint32_t output_row_pitch_in_blocks_or_pixels = 0, |
| 443 |
basisu_transcoder_state* pState = nullptr, uint32_t output_rows_in_pixels = 0, int channel0 = -1, int channel1 = -1, uint32_t decode_flags = 0) |
| 444 |
{ |
| 445 |
return transcode_slice(pDst_blocks, num_blocks_x, num_blocks_y, pImage_data, image_data_size, fmt, |
| 446 |
output_block_or_pixel_stride_in_bytes, bc1_allow_threecolor_blocks, (header.m_flags & cBASISHeaderFlagHasAlphaSlices) != 0, slice_desc.m_orig_width, slice_desc.m_orig_height, output_row_pitch_in_blocks_or_pixels, |
| 447 |
pState, output_rows_in_pixels, channel0, channel1, decode_flags); |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
bool transcode_image( |
| 452 |
transcoder_texture_format target_format, |
| 453 |
void* pOutput_blocks, uint32_t output_blocks_buf_size_in_blocks_or_pixels, |
| 454 |
const uint8_t* pCompressed_data, uint32_t compressed_data_length, |
| 455 |
uint32_t num_blocks_x, uint32_t num_blocks_y, uint32_t orig_width, uint32_t orig_height, uint32_t level_index, |
| 456 |
uint64_t slice_offset, uint32_t slice_length, |
| 457 |
uint32_t decode_flags = 0, |
| 458 |
bool has_alpha = false, |
| 459 |
bool is_video = false, |
| 460 |
uint32_t output_row_pitch_in_blocks_or_pixels = 0, |
| 461 |
basisu_transcoder_state* pState = nullptr, |
| 462 |
uint32_t output_rows_in_pixels = 0, |
| 463 |
int channel0 = -1, int channel1 = -1); |
| 464 |
}; |
| 465 |
|
| 466 |
struct basisu_slice_info |
| 467 |
{ |
| 468 |
uint32_t m_orig_width; |
| 469 |
uint32_t m_orig_height; |
| 470 |
|
| 471 |
uint32_t m_width; |
| 472 |
uint32_t m_height; |
| 473 |
|
| 474 |
uint32_t m_num_blocks_x; |
| 475 |
uint32_t m_num_blocks_y; |
| 476 |
uint32_t m_total_blocks; |
| 477 |
|
| 478 |
uint32_t m_block_width; |
| 479 |
uint32_t m_block_height; |
| 480 |
|
| 481 |
uint32_t m_compressed_size; |
| 482 |
|
| 483 |
uint32_t m_slice_index; |
| 484 |
uint32_t m_image_index; |
| 485 |
uint32_t m_level_index; |
| 486 |
|
| 487 |
uint32_t m_unpacked_slice_crc16; |
| 488 |
|
| 489 |
bool m_alpha_flag; |
| 490 |
bool m_iframe_flag; |
| 491 |
}; |
| 492 |
|
| 493 |
typedef basisu::vector<basisu_slice_info> basisu_slice_info_vec; |
| 494 |
|
| 495 |
struct basisu_image_info |
| 496 |
{ |
| 497 |
uint32_t m_image_index; |
| 498 |
uint32_t m_total_levels; |
| 499 |
|
| 500 |
uint32_t m_orig_width; |
| 501 |
uint32_t m_orig_height; |
| 502 |
|
| 503 |
uint32_t m_width; |
| 504 |
uint32_t m_height; |
| 505 |
|
| 506 |
uint32_t m_block_width; |
| 507 |
uint32_t m_block_height; |
| 508 |
|
| 509 |
uint32_t m_num_blocks_x; |
| 510 |
uint32_t m_num_blocks_y; |
| 511 |
uint32_t m_total_blocks; |
| 512 |
|
| 513 |
uint32_t m_first_slice_index; |
| 514 |
|
| 515 |
bool m_alpha_flag; |
| 516 |
bool m_iframe_flag; |
| 517 |
}; |
| 518 |
|
| 519 |
struct basisu_image_level_info |
| 520 |
{ |
| 521 |
uint32_t m_image_index; |
| 522 |
uint32_t m_level_index; |
| 523 |
|
| 524 |
uint32_t m_orig_width; |
| 525 |
uint32_t m_orig_height; |
| 526 |
|
| 527 |
uint32_t m_width; |
| 528 |
uint32_t m_height; |
| 529 |
|
| 530 |
uint32_t m_block_width; |
| 531 |
uint32_t m_block_height; |
| 532 |
|
| 533 |
uint32_t m_num_blocks_x; |
| 534 |
uint32_t m_num_blocks_y; |
| 535 |
uint32_t m_total_blocks; |
| 536 |
|
| 537 |
uint32_t m_first_slice_index; |
| 538 |
|
| 539 |
uint32_t m_rgb_file_ofs; |
| 540 |
uint32_t m_rgb_file_len; |
| 541 |
uint32_t m_alpha_file_ofs; |
| 542 |
uint32_t m_alpha_file_len; |
| 543 |
|
| 544 |
bool m_alpha_flag; |
| 545 |
bool m_iframe_flag; |
| 546 |
}; |
| 547 |
|
| 548 |
struct basisu_file_info |
| 549 |
{ |
| 550 |
uint32_t m_version; |
| 551 |
uint32_t m_total_header_size; |
| 552 |
|
| 553 |
uint32_t m_total_selectors; |
| 554 |
|
| 555 |
uint32_t m_selector_codebook_ofs; |
| 556 |
uint32_t m_selector_codebook_size; |
| 557 |
|
| 558 |
uint32_t m_total_endpoints; |
| 559 |
|
| 560 |
uint32_t m_endpoint_codebook_ofs; |
| 561 |
uint32_t m_endpoint_codebook_size; |
| 562 |
|
| 563 |
uint32_t m_tables_ofs; |
| 564 |
uint32_t m_tables_size; |
| 565 |
|
| 566 |
uint32_t m_slices_size; |
| 567 |
|
| 568 |
basis_texture_type m_tex_type; |
| 569 |
uint32_t m_us_per_frame; |
| 570 |
|
| 571 |
|
| 572 |
basisu_slice_info_vec m_slice_info; |
| 573 |
|
| 574 |
uint32_t m_total_images; |
| 575 |
basisu::vector<uint32_t> m_image_mipmap_levels; |
| 576 |
|
| 577 |
uint32_t m_userdata0; |
| 578 |
uint32_t m_userdata1; |
| 579 |
|
| 580 |
basis_tex_format m_tex_format; |
| 581 |
|
| 582 |
uint32_t m_block_width; |
| 583 |
uint32_t m_block_height; |
| 584 |
|
| 585 |
bool m_y_flipped; |
| 586 |
bool m_etc1s; |
| 587 |
bool m_has_alpha_slices; |
| 588 |
}; |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
class basisu_transcoder |
| 593 |
{ |
| 594 |
basisu_transcoder(basisu_transcoder&); |
| 595 |
basisu_transcoder& operator= (const basisu_transcoder&); |
| 596 |
|
| 597 |
public: |
| 598 |
basisu_transcoder(); |
| 599 |
|
| 600 |
|
| 601 |
bool validate_file_checksums(const void* pData, uint32_t data_size, bool full_validation) const; |
| 602 |
|
| 603 |
|
| 604 |
bool validate_header(const void* pData, uint32_t data_size) const; |
| 605 |
|
| 606 |
basis_texture_type get_texture_type(const void* pData, uint32_t data_size) const; |
| 607 |
bool get_userdata(const void* pData, uint32_t data_size, uint32_t& userdata0, uint32_t& userdata1) const; |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
uint32_t get_total_images(const void* pData, uint32_t data_size) const; |
| 612 |
|
| 613 |
basis_tex_format get_basis_tex_format(const void* pData, uint32_t data_size) const; |
| 614 |
|
| 615 |
|
| 616 |
uint32_t get_total_image_levels(const void* pData, uint32_t data_size, uint32_t image_index) const; |
| 617 |
|
| 618 |
|
| 619 |
bool get_image_level_desc(const void* pData, uint32_t data_size, uint32_t image_index, uint32_t level_index, uint32_t& orig_width, uint32_t& orig_height, uint32_t& total_blocks) const; |
| 620 |
|
| 621 |
|
| 622 |
bool get_image_info(const void* pData, uint32_t data_size, basisu_image_info& image_info, uint32_t image_index) const; |
| 623 |
|
| 624 |
|
| 625 |
bool get_image_level_info(const void* pData, uint32_t data_size, basisu_image_level_info& level_info, uint32_t image_index, uint32_t level_index) const; |
| 626 |
|
| 627 |
|
| 628 |
bool get_file_info(const void* pData, uint32_t data_size, basisu_file_info& file_info) const; |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
bool start_transcoding(const void* pData, uint32_t data_size); |
| 633 |
|
| 634 |
bool stop_transcoding(); |
| 635 |
|
| 636 |
|
| 637 |
bool get_ready_to_transcode() const { return m_ready_to_transcode; } |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
bool transcode_image_level( |
| 651 |
const void* pData, uint32_t data_size, |
| 652 |
uint32_t image_index, uint32_t level_index, |
| 653 |
void* pOutput_blocks, uint32_t output_blocks_buf_size_in_blocks_or_pixels, |
| 654 |
transcoder_texture_format fmt, |
| 655 |
uint32_t decode_flags = 0, uint32_t output_row_pitch_in_blocks_or_pixels = 0, basisu_transcoder_state* pState = nullptr, uint32_t output_rows_in_pixels = 0) const; |
| 656 |
|
| 657 |
|
| 658 |
int find_slice(const void* pData, uint32_t data_size, uint32_t image_index, uint32_t level_index, bool alpha_data) const; |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
bool transcode_slice(const void* pData, uint32_t data_size, uint32_t slice_index, |
| 670 |
void* pOutput_blocks, uint32_t output_blocks_buf_size_in_blocks_or_pixels, |
| 671 |
block_format fmt, uint32_t output_block_stride_in_bytes, uint32_t decode_flags = 0, uint32_t output_row_pitch_in_blocks_or_pixels = 0, basisu_transcoder_state* pState = nullptr, void* pAlpha_blocks = nullptr, |
| 672 |
uint32_t output_rows_in_pixels = 0, int channel0 = -1, int channel1 = -1) const; |
| 673 |
|
| 674 |
static void write_opaque_alpha_blocks( |
| 675 |
uint32_t num_blocks_x, uint32_t num_blocks_y, |
| 676 |
void* pOutput_blocks, block_format fmt, |
| 677 |
uint32_t block_stride_in_bytes, uint32_t output_row_pitch_in_blocks_or_pixels); |
| 678 |
|
| 679 |
void set_global_codebooks(const basisu_lowlevel_etc1s_transcoder* pGlobal_codebook) { m_lowlevel_etc1s_decoder.set_global_codebooks(pGlobal_codebook); } |
| 680 |
const basisu_lowlevel_etc1s_transcoder* get_global_codebooks() const { return m_lowlevel_etc1s_decoder.get_global_codebooks(); } |
| 681 |
|
| 682 |
const basisu_lowlevel_etc1s_transcoder& get_lowlevel_etc1s_decoder() const { return m_lowlevel_etc1s_decoder; } |
| 683 |
basisu_lowlevel_etc1s_transcoder& get_lowlevel_etc1s_decoder() { return m_lowlevel_etc1s_decoder; } |
| 684 |
|
| 685 |
const basisu_lowlevel_uastc_ldr_4x4_transcoder& get_lowlevel_uastc_decoder() const { return m_lowlevel_uastc_decoder; } |
| 686 |
basisu_lowlevel_uastc_ldr_4x4_transcoder& get_lowlevel_uastc_decoder() { return m_lowlevel_uastc_decoder; } |
| 687 |
|
| 688 |
private: |
| 689 |
mutable basisu_lowlevel_etc1s_transcoder m_lowlevel_etc1s_decoder; |
| 690 |
mutable basisu_lowlevel_uastc_ldr_4x4_transcoder m_lowlevel_uastc_decoder; |
| 691 |
mutable basisu_lowlevel_uastc_hdr_4x4_transcoder m_lowlevel_uastc_4x4_hdr_decoder; |
| 692 |
mutable basisu_lowlevel_astc_hdr_6x6_transcoder m_lowlevel_astc_6x6_hdr_decoder; |
| 693 |
mutable basisu_lowlevel_astc_hdr_6x6_intermediate_transcoder m_lowlevel_astc_6x6_hdr_intermediate_decoder; |
| 694 |
|
| 695 |
bool m_ready_to_transcode; |
| 696 |
|
| 697 |
int find_first_slice_index(const void* pData, uint32_t data_size, uint32_t image_index, uint32_t level_index) const; |
| 698 |
|
| 699 |
bool validate_header_quick(const void* pData, uint32_t data_size) const; |
| 700 |
}; |
| 701 |
|
| 702 |
|
| 703 |
void basisu_transcoder_init(); |
| 704 |
|
| 705 |
enum debug_flags_t |
| 706 |
{ |
| 707 |
cDebugFlagVisCRs = 1, |
| 708 |
cDebugFlagVisBC1Sels = 2, |
| 709 |
cDebugFlagVisBC1Endpoints = 4 |
| 710 |
}; |
| 711 |
uint32_t get_debug_flags(); |
| 712 |
void set_debug_flags(uint32_t f); |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
#if BASISD_SUPPORT_KTX2 |
| 719 |
#pragma pack(push) |
| 720 |
#pragma pack(1) |
| 721 |
struct ktx2_header |
| 722 |
{ |
| 723 |
uint8_t m_identifier[12]; |
| 724 |
basisu::packed_uint<4> m_vk_format; |
| 725 |
basisu::packed_uint<4> m_type_size; |
| 726 |
basisu::packed_uint<4> m_pixel_width; |
| 727 |
basisu::packed_uint<4> m_pixel_height; |
| 728 |
basisu::packed_uint<4> m_pixel_depth; |
| 729 |
basisu::packed_uint<4> m_layer_count; |
| 730 |
basisu::packed_uint<4> m_face_count; |
| 731 |
basisu::packed_uint<4> m_level_count; |
| 732 |
basisu::packed_uint<4> m_supercompression_scheme; |
| 733 |
basisu::packed_uint<4> m_dfd_byte_offset; |
| 734 |
basisu::packed_uint<4> m_dfd_byte_length; |
| 735 |
basisu::packed_uint<4> m_kvd_byte_offset; |
| 736 |
basisu::packed_uint<4> m_kvd_byte_length; |
| 737 |
basisu::packed_uint<8> m_sgd_byte_offset; |
| 738 |
basisu::packed_uint<8> m_sgd_byte_length; |
| 739 |
}; |
| 740 |
|
| 741 |
struct ktx2_level_index |
| 742 |
{ |
| 743 |
basisu::packed_uint<8> m_byte_offset; |
| 744 |
basisu::packed_uint<8> m_byte_length; |
| 745 |
basisu::packed_uint<8> m_uncompressed_byte_length; |
| 746 |
}; |
| 747 |
|
| 748 |
struct ktx2_etc1s_global_data_header |
| 749 |
{ |
| 750 |
basisu::packed_uint<2> m_endpoint_count; |
| 751 |
basisu::packed_uint<2> m_selector_count; |
| 752 |
basisu::packed_uint<4> m_endpoints_byte_length; |
| 753 |
basisu::packed_uint<4> m_selectors_byte_length; |
| 754 |
basisu::packed_uint<4> m_tables_byte_length; |
| 755 |
basisu::packed_uint<4> m_extended_byte_length; |
| 756 |
}; |
| 757 |
|
| 758 |
struct ktx2_etc1s_image_desc |
| 759 |
{ |
| 760 |
basisu::packed_uint<4> m_image_flags; |
| 761 |
basisu::packed_uint<4> m_rgb_slice_byte_offset; |
| 762 |
basisu::packed_uint<4> m_rgb_slice_byte_length; |
| 763 |
basisu::packed_uint<4> m_alpha_slice_byte_offset; |
| 764 |
basisu::packed_uint<4> m_alpha_slice_byte_length; |
| 765 |
}; |
| 766 |
|
| 767 |
struct ktx2_astc_hdr_6x6_intermediate_image_desc |
| 768 |
{ |
| 769 |
basisu::packed_uint<4> m_rgb_slice_byte_offset; |
| 770 |
basisu::packed_uint<4> m_rgb_slice_byte_length; |
| 771 |
}; |
| 772 |
|
| 773 |
struct ktx2_animdata |
| 774 |
{ |
| 775 |
basisu::packed_uint<4> m_duration; |
| 776 |
basisu::packed_uint<4> m_timescale; |
| 777 |
basisu::packed_uint<4> m_loopcount; |
| 778 |
}; |
| 779 |
#pragma pack(pop) |
| 780 |
|
| 781 |
const uint32_t KTX2_VK_FORMAT_UNDEFINED = 0; |
| 782 |
|
| 783 |
|
| 784 |
const uint32_t KTX2_FORMAT_ASTC_4x4_SFLOAT_BLOCK = 1000066000; |
| 785 |
const uint32_t KTX2_FORMAT_ASTC_5x4_SFLOAT_BLOCK = 1000066001; |
| 786 |
const uint32_t KTX2_FORMAT_ASTC_5x5_SFLOAT_BLOCK = 1000066002; |
| 787 |
const uint32_t KTX2_FORMAT_ASTC_6x5_SFLOAT_BLOCK = 1000066003; |
| 788 |
const uint32_t KTX2_FORMAT_ASTC_6x6_SFLOAT_BLOCK = 1000066004; |
| 789 |
const uint32_t KTX2_FORMAT_ASTC_8x5_SFLOAT_BLOCK = 1000066005; |
| 790 |
const uint32_t KTX2_FORMAT_ASTC_8x6_SFLOAT_BLOCK = 1000066006; |
| 791 |
|
| 792 |
const uint32_t KTX2_KDF_DF_MODEL_ASTC = 162; |
| 793 |
const uint32_t KTX2_KDF_DF_MODEL_ETC1S = 163; |
| 794 |
const uint32_t KTX2_KDF_DF_MODEL_UASTC_LDR_4X4 = 166; |
| 795 |
const uint32_t KTX2_KDF_DF_MODEL_UASTC_HDR_4X4 = 167; |
| 796 |
const uint32_t KTX2_KDF_DF_MODEL_ASTC_HDR_6X6_INTERMEDIATE = 168; |
| 797 |
|
| 798 |
const uint32_t KTX2_IMAGE_IS_P_FRAME = 2; |
| 799 |
const uint32_t KTX2_UASTC_BLOCK_SIZE = 16; |
| 800 |
const uint32_t KTX2_MAX_SUPPORTED_LEVEL_COUNT = 16; |
| 801 |
|
| 802 |
|
| 803 |
const uint32_t KTX2_KHR_DF_TRANSFER_LINEAR = 1; |
| 804 |
const uint32_t KTX2_KHR_DF_TRANSFER_SRGB = 2; |
| 805 |
|
| 806 |
enum ktx2_supercompression |
| 807 |
{ |
| 808 |
KTX2_SS_NONE = 0, |
| 809 |
KTX2_SS_BASISLZ = 1, |
| 810 |
KTX2_SS_ZSTANDARD = 2, |
| 811 |
KTX2_SS_BASIS |
| 812 |
}; |
| 813 |
|
| 814 |
extern const uint8_t g_ktx2_file_identifier[12]; |
| 815 |
|
| 816 |
enum ktx2_df_channel_id |
| 817 |
{ |
| 818 |
KTX2_DF_CHANNEL_ETC1S_RGB = 0U, |
| 819 |
KTX2_DF_CHANNEL_ETC1S_RRR = 3U, |
| 820 |
KTX2_DF_CHANNEL_ETC1S_GGG = 4U, |
| 821 |
KTX2_DF_CHANNEL_ETC1S_AAA = 15U, |
| 822 |
|
| 823 |
KTX2_DF_CHANNEL_UASTC_DATA = 0U, |
| 824 |
KTX2_DF_CHANNEL_UASTC_RGB = 0U, |
| 825 |
KTX2_DF_CHANNEL_UASTC_RGBA = 3U, |
| 826 |
KTX2_DF_CHANNEL_UASTC_RRR = 4U, |
| 827 |
KTX2_DF_CHANNEL_UASTC_RRRG = 5U, |
| 828 |
KTX2_DF_CHANNEL_UASTC_RG = 6U, |
| 829 |
}; |
| 830 |
|
| 831 |
inline const char* ktx2_get_etc1s_df_channel_id_str(ktx2_df_channel_id id) |
| 832 |
{ |
| 833 |
switch (id) |
| 834 |
{ |
| 835 |
case KTX2_DF_CHANNEL_ETC1S_RGB: return "RGB"; |
| 836 |
case KTX2_DF_CHANNEL_ETC1S_RRR: return "RRR"; |
| 837 |
case KTX2_DF_CHANNEL_ETC1S_GGG: return "GGG"; |
| 838 |
case KTX2_DF_CHANNEL_ETC1S_AAA: return "AAA"; |
| 839 |
default: break; |
| 840 |
} |
| 841 |
return "?"; |
| 842 |
} |
| 843 |
|
| 844 |
inline const char* ktx2_get_uastc_df_channel_id_str(ktx2_df_channel_id id) |
| 845 |
{ |
| 846 |
switch (id) |
| 847 |
{ |
| 848 |
case KTX2_DF_CHANNEL_UASTC_RGB: return "RGB"; |
| 849 |
case KTX2_DF_CHANNEL_UASTC_RGBA: return "RGBA"; |
| 850 |
case KTX2_DF_CHANNEL_UASTC_RRR: return "RRR"; |
| 851 |
case KTX2_DF_CHANNEL_UASTC_RRRG: return "RRRG"; |
| 852 |
case KTX2_DF_CHANNEL_UASTC_RG: return "RG"; |
| 853 |
default: break; |
| 854 |
} |
| 855 |
return "?"; |
| 856 |
} |
| 857 |
|
| 858 |
enum ktx2_df_color_primaries |
| 859 |
{ |
| 860 |
KTX2_DF_PRIMARIES_UNSPECIFIED = 0, |
| 861 |
KTX2_DF_PRIMARIES_BT709 = 1, |
| 862 |
KTX2_DF_PRIMARIES_SRGB = 1, |
| 863 |
KTX2_DF_PRIMARIES_BT601_EBU = 2, |
| 864 |
KTX2_DF_PRIMARIES_BT601_SMPTE = 3, |
| 865 |
KTX2_DF_PRIMARIES_BT2020 = 4, |
| 866 |
KTX2_DF_PRIMARIES_CIEXYZ = 5, |
| 867 |
KTX2_DF_PRIMARIES_ACES = 6, |
| 868 |
KTX2_DF_PRIMARIES_ACESCC = 7, |
| 869 |
KTX2_DF_PRIMARIES_NTSC1953 = 8, |
| 870 |
KTX2_DF_PRIMARIES_PAL525 = 9, |
| 871 |
KTX2_DF_PRIMARIES_DISPLAYP3 = 10, |
| 872 |
KTX2_DF_PRIMARIES_ADOBERGB = 11 |
| 873 |
}; |
| 874 |
|
| 875 |
inline const char* ktx2_get_df_color_primaries_str(ktx2_df_color_primaries p) |
| 876 |
{ |
| 877 |
switch (p) |
| 878 |
{ |
| 879 |
case KTX2_DF_PRIMARIES_UNSPECIFIED: return "UNSPECIFIED"; |
| 880 |
case KTX2_DF_PRIMARIES_BT709: return "BT709"; |
| 881 |
case KTX2_DF_PRIMARIES_BT601_EBU: return "EBU"; |
| 882 |
case KTX2_DF_PRIMARIES_BT601_SMPTE: return "SMPTE"; |
| 883 |
case KTX2_DF_PRIMARIES_BT2020: return "BT2020"; |
| 884 |
case KTX2_DF_PRIMARIES_CIEXYZ: return "CIEXYZ"; |
| 885 |
case KTX2_DF_PRIMARIES_ACES: return "ACES"; |
| 886 |
case KTX2_DF_PRIMARIES_ACESCC: return "ACESCC"; |
| 887 |
case KTX2_DF_PRIMARIES_NTSC1953: return "NTSC1953"; |
| 888 |
case KTX2_DF_PRIMARIES_PAL525: return "PAL525"; |
| 889 |
case KTX2_DF_PRIMARIES_DISPLAYP3: return "DISPLAYP3"; |
| 890 |
case KTX2_DF_PRIMARIES_ADOBERGB: return "ADOBERGB"; |
| 891 |
default: break; |
| 892 |
} |
| 893 |
return "?"; |
| 894 |
} |
| 895 |
|
| 896 |
|
| 897 |
struct ktx2_image_level_info |
| 898 |
{ |
| 899 |
|
| 900 |
uint32_t m_level_index; |
| 901 |
uint32_t m_layer_index; |
| 902 |
uint32_t m_face_index; |
| 903 |
|
| 904 |
|
| 905 |
uint32_t m_orig_width; |
| 906 |
uint32_t m_orig_height; |
| 907 |
|
| 908 |
|
| 909 |
uint32_t m_width; |
| 910 |
uint32_t m_height; |
| 911 |
|
| 912 |
|
| 913 |
uint32_t m_num_blocks_x; |
| 914 |
uint32_t m_num_blocks_y; |
| 915 |
|
| 916 |
|
| 917 |
uint32_t m_block_width; |
| 918 |
uint32_t m_block_height; |
| 919 |
|
| 920 |
|
| 921 |
uint32_t m_total_blocks; |
| 922 |
|
| 923 |
|
| 924 |
bool m_alpha_flag; |
| 925 |
|
| 926 |
|
| 927 |
bool m_iframe_flag; |
| 928 |
}; |
| 929 |
|
| 930 |
|
| 931 |
struct ktx2_transcoder_state |
| 932 |
{ |
| 933 |
basist::basisu_transcoder_state m_transcoder_state; |
| 934 |
basisu::uint8_vec m_level_uncomp_data; |
| 935 |
int m_uncomp_data_level_index; |
| 936 |
|
| 937 |
void clear() |
| 938 |
{ |
| 939 |
m_transcoder_state.clear(); |
| 940 |
m_level_uncomp_data.clear(); |
| 941 |
m_uncomp_data_level_index = -1; |
| 942 |
} |
| 943 |
}; |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
class ktx2_transcoder |
| 952 |
{ |
| 953 |
public: |
| 954 |
ktx2_transcoder(); |
| 955 |
|
| 956 |
|
| 957 |
void clear(); |
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
bool init(const void* pData, uint32_t data_size); |
| 963 |
|
| 964 |
|
| 965 |
const uint8_t* get_data() const { return m_pData; } |
| 966 |
uint32_t get_data_size() const { return m_data_size; } |
| 967 |
|
| 968 |
|
| 969 |
const ktx2_header& get_header() const { return m_header; } |
| 970 |
|
| 971 |
|
| 972 |
const basisu::vector<ktx2_level_index>& get_level_index() const { return m_levels; } |
| 973 |
|
| 974 |
|
| 975 |
uint32_t get_width() const { return m_header.m_pixel_width; } |
| 976 |
|
| 977 |
|
| 978 |
uint32_t get_height() const { return m_header.m_pixel_height; } |
| 979 |
|
| 980 |
|
| 981 |
uint32_t get_levels() const { return m_header.m_level_count; } |
| 982 |
|
| 983 |
|
| 984 |
uint32_t get_faces() const { return m_header.m_face_count; } |
| 985 |
|
| 986 |
|
| 987 |
uint32_t get_layers() const { return m_header.m_layer_count; } |
| 988 |
|
| 989 |
|
| 990 |
basist::basis_tex_format get_basis_tex_format() const { return m_format; } |
| 991 |
|
| 992 |
|
| 993 |
bool is_etc1s() const { return get_basis_tex_format() == basist::basis_tex_format::cETC1S; } |
| 994 |
|
| 995 |
|
| 996 |
bool is_uastc() const { return get_basis_tex_format() == basist::basis_tex_format::cUASTC4x4; } |
| 997 |
|
| 998 |
|
| 999 |
bool is_hdr() const |
| 1000 |
{ |
| 1001 |
return basis_tex_format_is_hdr(get_basis_tex_format()); |
| 1002 |
} |
| 1003 |
|
| 1004 |
bool is_ldr() const |
| 1005 |
{ |
| 1006 |
return !is_hdr(); |
| 1007 |
} |
| 1008 |
|
| 1009 |
bool is_hdr_4x4() const |
| 1010 |
{ |
| 1011 |
return (get_basis_tex_format() == basist::basis_tex_format::cUASTC_HDR_4x4); |
| 1012 |
} |
| 1013 |
|
| 1014 |
bool is_hdr_6x6() const |
| 1015 |
{ |
| 1016 |
return (get_basis_tex_format() == basist::basis_tex_format::cASTC_HDR_6x6) || (get_basis_tex_format() == basist::basis_tex_format::cASTC_HDR_6x6_INTERMEDIATE); |
| 1017 |
} |
| 1018 |
|
| 1019 |
uint32_t get_block_width() const { return basis_tex_format_get_block_width(get_basis_tex_format()); } |
| 1020 |
uint32_t get_block_height() const { return basis_tex_format_get_block_height(get_basis_tex_format()); } |
| 1021 |
|
| 1022 |
|
| 1023 |
uint32_t get_has_alpha() const { return m_has_alpha; } |
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
const basisu::uint8_vec& get_dfd() const { return m_dfd; } |
| 1028 |
|
| 1029 |
|
| 1030 |
uint32_t get_dfd_color_model() const { return m_dfd_color_model; } |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
ktx2_df_color_primaries get_dfd_color_primaries() const { return m_dfd_color_prims; } |
| 1035 |
|
| 1036 |
|
| 1037 |
uint32_t get_dfd_transfer_func() const { return m_dfd_transfer_func; } |
| 1038 |
|
| 1039 |
uint32_t get_dfd_flags() const { return m_dfd_flags; } |
| 1040 |
|
| 1041 |
|
| 1042 |
uint32_t get_dfd_total_samples() const { return m_dfd_samples; } |
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
ktx2_df_channel_id get_dfd_channel_id0() const { return m_dfd_chan0; } |
| 1048 |
ktx2_df_channel_id get_dfd_channel_id1() const { return m_dfd_chan1; } |
| 1049 |
|
| 1050 |
|
| 1051 |
struct key_value |
| 1052 |
{ |
| 1053 |
|
| 1054 |
|
| 1055 |
basisu::uint8_vec m_key; |
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
basisu::uint8_vec m_value; |
| 1060 |
|
| 1061 |
bool operator< (const key_value& rhs) const { return strcmp((const char*)m_key.data(), (const char *)rhs.m_key.data()) < 0; } |
| 1062 |
}; |
| 1063 |
typedef basisu::vector<key_value> key_value_vec; |
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
const key_value_vec& get_key_values() const { return m_key_values; } |
| 1068 |
|
| 1069 |
const basisu::uint8_vec *find_key(const std::string& key_name) const; |
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
const ktx2_etc1s_global_data_header& get_etc1s_header() const { return m_etc1s_header; } |
| 1075 |
|
| 1076 |
|
| 1077 |
const basisu::vector<ktx2_etc1s_image_desc>& get_etc1s_image_descs() const { return m_etc1s_image_descs; } |
| 1078 |
|
| 1079 |
const basisu::vector<ktx2_astc_hdr_6x6_intermediate_image_desc>& get_astc_hdr_6x6_intermediate_image_descs() const { return m_astc_6x6_intermediate_image_descs; } |
| 1080 |
|
| 1081 |
|
| 1082 |
uint32_t get_etc1s_image_descs_image_flags(uint32_t level_index, uint32_t layer_index, uint32_t face_index) const; |
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
bool is_video() const { return m_is_video; } |
| 1087 |
|
| 1088 |
|
| 1089 |
float get_ldr_hdr_upconversion_nit_multiplier() const { return m_ldr_hdr_upconversion_nit_multiplier; } |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
bool start_transcoding(); |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
bool get_image_level_info(ktx2_image_level_info& level_info, uint32_t level_index, uint32_t layer_index, uint32_t face_index) const; |
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
bool transcode_image_level( |
| 1106 |
uint32_t level_index, uint32_t layer_index, uint32_t face_index, |
| 1107 |
void* pOutput_blocks, uint32_t output_blocks_buf_size_in_blocks_or_pixels, |
| 1108 |
basist::transcoder_texture_format fmt, |
| 1109 |
uint32_t decode_flags = 0, uint32_t output_row_pitch_in_blocks_or_pixels = 0, uint32_t output_rows_in_pixels = 0, int channel0 = -1, int channel1 = -1, |
| 1110 |
ktx2_transcoder_state *pState = nullptr); |
| 1111 |
|
| 1112 |
private: |
| 1113 |
const uint8_t* m_pData; |
| 1114 |
uint32_t m_data_size; |
| 1115 |
|
| 1116 |
ktx2_header m_header; |
| 1117 |
basisu::vector<ktx2_level_index> m_levels; |
| 1118 |
basisu::uint8_vec m_dfd; |
| 1119 |
key_value_vec m_key_values; |
| 1120 |
|
| 1121 |
ktx2_etc1s_global_data_header m_etc1s_header; |
| 1122 |
basisu::vector<ktx2_etc1s_image_desc> m_etc1s_image_descs; |
| 1123 |
basisu::vector<ktx2_astc_hdr_6x6_intermediate_image_desc> m_astc_6x6_intermediate_image_descs; |
| 1124 |
|
| 1125 |
basist::basis_tex_format m_format; |
| 1126 |
|
| 1127 |
uint32_t m_dfd_color_model; |
| 1128 |
ktx2_df_color_primaries m_dfd_color_prims; |
| 1129 |
uint32_t m_dfd_transfer_func; |
| 1130 |
uint32_t m_dfd_flags; |
| 1131 |
uint32_t m_dfd_samples; |
| 1132 |
ktx2_df_channel_id m_dfd_chan0, m_dfd_chan1; |
| 1133 |
|
| 1134 |
basist::basisu_lowlevel_etc1s_transcoder m_etc1s_transcoder; |
| 1135 |
basist::basisu_lowlevel_uastc_ldr_4x4_transcoder m_uastc_transcoder; |
| 1136 |
basist::basisu_lowlevel_uastc_hdr_4x4_transcoder m_uastc_hdr_transcoder; |
| 1137 |
basist::basisu_lowlevel_astc_hdr_6x6_transcoder m_astc_hdr_6x6_transcoder; |
| 1138 |
basist::basisu_lowlevel_astc_hdr_6x6_intermediate_transcoder m_astc_hdr_6x6_intermediate_transcoder; |
| 1139 |
|
| 1140 |
ktx2_transcoder_state m_def_transcoder_state; |
| 1141 |
|
| 1142 |
bool m_has_alpha; |
| 1143 |
bool m_is_video; |
| 1144 |
float m_ldr_hdr_upconversion_nit_multiplier; |
| 1145 |
|
| 1146 |
bool decompress_level_data(uint32_t level_index, basisu::uint8_vec& uncomp_data); |
| 1147 |
bool read_astc_6x6_hdr_intermediate_global_data(); |
| 1148 |
bool decompress_etc1s_global_data(); |
| 1149 |
bool read_key_values(); |
| 1150 |
}; |
| 1151 |
|
| 1152 |
|
| 1153 |
inline void ktx2_add_key_value(ktx2_transcoder::key_value_vec& key_values, const std::string& key, const std::string& val) |
| 1154 |
{ |
| 1155 |
assert(key.size()); |
| 1156 |
|
| 1157 |
basist::ktx2_transcoder::key_value* p = nullptr; |
| 1158 |
|
| 1159 |
|
| 1160 |
for (size_t i = 0; i < key_values.size(); i++) |
| 1161 |
{ |
| 1162 |
if (strcmp((const char*)key_values[i].m_key.data(), key.c_str()) == 0) |
| 1163 |
{ |
| 1164 |
p = &key_values[i]; |
| 1165 |
break; |
| 1166 |
} |
| 1167 |
} |
| 1168 |
|
| 1169 |
if (!p) |
| 1170 |
p = key_values.enlarge(1); |
| 1171 |
|
| 1172 |
p->m_key.resize(0); |
| 1173 |
p->m_value.resize(0); |
| 1174 |
|
| 1175 |
p->m_key.resize(key.size() + 1); |
| 1176 |
memcpy(p->m_key.data(), key.c_str(), key.size()); |
| 1177 |
|
| 1178 |
p->m_value.resize(val.size() + 1); |
| 1179 |
if (val.size()) |
| 1180 |
memcpy(p->m_value.data(), val.c_str(), val.size()); |
| 1181 |
} |
| 1182 |
|
| 1183 |
#endif |
| 1184 |
|
| 1185 |
|
| 1186 |
bool basisu_transcoder_supports_ktx2(); |
| 1187 |
|
| 1188 |
|
| 1189 |
bool basisu_transcoder_supports_ktx2_zstd(); |
| 1190 |
|
| 1191 |
} |
| 1192 |
|