basisu_transcoder.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // basisu_transcoder.h
  2. // Copyright (C) 2019 Binomial LLC. All Rights Reserved.
  3. // Important: If compiling with gcc, be sure strict aliasing is disabled: -fno-strict-aliasing
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. #pragma once
  17. // Set BASISU_DEVEL_MESSAGES to 1 to enable debug printf()'s whenever an error occurs, for easier debugging during development.
  18. //#define BASISU_DEVEL_MESSAGES 1
  19. #include "basisu_transcoder_internal.h"
  20. #include "basisu_global_selector_palette.h"
  21. #include "basisu_file_headers.h"
  22. namespace basist
  23. {
  24. // High-level composite texture formats supported by the transcoder.
  25. // Each of these texture formats directly correspond to OpenGL/D3D/Vulkan etc. texture formats.
  26. // Notes:
  27. // - If you specify a texture format that supports alpha, but the .basis file doesn't have alpha, the transcoder will automatically output a
  28. // fully opaque (255) alpha channel.
  29. // - The PVRTC1 texture formats only support power of 2 dimension .basis files, but this may be relaxed in a future version.
  30. // - The PVRTC1 transcoders are real-time encoders, so don't expect the highest quality. We may add a slower encoder with improved quality.
  31. // - These enums must be kept in sync with Javascript code that calls the transcoder.
  32. enum class transcoder_texture_format
  33. {
  34. // Compressed formats
  35. // ETC1-2
  36. cTFETC1_RGB = 0, // Opaque only, returns RGB or alpha data if cDecodeFlagsTranscodeAlphaDataToOpaqueFormats flag is specified
  37. cTFETC2_RGBA = 1, // Opaque+alpha, ETC2_EAC_A8 block followed by a ETC1 block, alpha channel will be opaque for opaque .basis files
  38. // BC1-5, BC7 (desktop, some mobile devices)
  39. cTFBC1_RGB = 2, // Opaque only, no punchthrough alpha support yet, transcodes alpha slice if cDecodeFlagsTranscodeAlphaDataToOpaqueFormats flag is specified
  40. cTFBC3_RGBA = 3, // Opaque+alpha, BC4 followed by a BC1 block, alpha channel will be opaque for opaque .basis files
  41. cTFBC4_R = 4, // Red only, alpha slice is transcoded to output if cDecodeFlagsTranscodeAlphaDataToOpaqueFormats flag is specified
  42. cTFBC5_RG = 5, // XY: Two BC4 blocks, X=R and Y=Alpha, .basis file should have alpha data (if not Y will be all 255's)
  43. cTFBC7_M6_RGB = 6, // Opaque only, RGB or alpha if cDecodeFlagsTranscodeAlphaDataToOpaqueFormats flag is specified. Highest quality of all the non-ETC1 formats.
  44. cTFBC7_M5_RGBA = 7, // Opaque+alpha, alpha channel will be opaque for opaque .basis files
  45. // PVRTC1 4bpp (mobile, PowerVR devices)
  46. cTFPVRTC1_4_RGB = 8, // Opaque only, RGB or alpha if cDecodeFlagsTranscodeAlphaDataToOpaqueFormats flag is specified, nearly lowest quality of any texture format.
  47. cTFPVRTC1_4_RGBA = 9, // Opaque+alpha, most useful for simple opacity maps. If .basis file doens't have alpha cTFPVRTC1_4_RGB will be used instead. Lowest quality of any supported texture format.
  48. // ASTC (mobile, Intel devices, hopefully all desktop GPU's one day)
  49. cTFASTC_4x4_RGBA = 10, // Opaque+alpha, ASTC 4x4, alpha channel will be opaque for opaque .basis files. Transcoder uses RGB/RGBA/L/LA modes, void extent, and up to two ([0,47] and [0,255]) endpoint precisions.
  50. // ATC (mobile, Adreno devices, this is a niche format)
  51. cTFATC_RGB = 11, // Opaque, RGB or alpha if cDecodeFlagsTranscodeAlphaDataToOpaqueFormats flag is specified. ATI ATC (GL_ATC_RGB_AMD)
  52. cTFATC_RGBA = 12, // Opaque+alpha, alpha channel will be opaque for opaque .basis files. ATI ATC (GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD)
  53. // FXT1 (desktop, Intel devices, this is a super obscure format)
  54. cTFFXT1_RGB = 17, // Opaque only, uses exclusively CC_MIXED blocks. Notable for having a 8x4 block size. GL_3DFX_texture_compression_FXT1 is supported on Intel integrated GPU's (such as HD 630).
  55. // Punch-through alpha is relatively easy to support, but full alpha is harder. This format is only here for completeness so opaque-only is fine for now.
  56. // See the BASISU_USE_ORIGINAL_3DFX_FXT1_ENCODING macro in basisu_transcoder_internal.h.
  57. cTFPVRTC2_4_RGB = 18, // Opaque-only, almost BC1 quality, much faster to transcode and supports arbitrary texture dimensions (unlike PVRTC1 RGB).
  58. cTFPVRTC2_4_RGBA = 19, // Opaque+alpha, slower to encode than cTFPVRTC2_4_RGB. Premultiplied alpha is highly recommended, otherwise the color channel can leak into the alpha channel on transparent blocks.
  59. cTFETC2_EAC_R11 = 20, // R only (ETC2 EAC R11 unsigned)
  60. cTFETC2_EAC_RG11 = 21, // RG only (ETC2 EAC RG11 unsigned), R=opaque.r, G=alpha - for tangent space normal maps
  61. // Uncompressed (raw pixel) formats
  62. cTFRGBA32 = 13, // 32bpp RGBA image stored in raster (not block) order in memory, R is first byte, A is last byte.
  63. cTFRGB565 = 14, // 166pp RGB image stored in raster (not block) order in memory, R at bit position 11
  64. cTFBGR565 = 15, // 16bpp RGB image stored in raster (not block) order in memory, R at bit position 0
  65. cTFRGBA4444 = 16, // 16bpp RGBA image stored in raster (not block) order in memory, R at bit position 12, A at bit position 0
  66. cTFTotalTextureFormats = 22,
  67. // Old enums for compatibility with code compiled against previous versions
  68. cTFETC1 = cTFETC1_RGB,
  69. cTFETC2 = cTFETC2_RGBA,
  70. cTFBC1 = cTFBC1_RGB,
  71. cTFBC3 = cTFBC3_RGBA,
  72. cTFBC4 = cTFBC4_R,
  73. cTFBC5 = cTFBC5_RG,
  74. cTFBC7_M6_OPAQUE_ONLY = cTFBC7_M6_RGB,
  75. cTFBC7_M5 = cTFBC7_M5_RGBA,
  76. cTFASTC_4x4 = cTFASTC_4x4_RGBA,
  77. cTFATC_RGBA_INTERPOLATED_ALPHA = cTFATC_RGBA,
  78. };
  79. uint32_t basis_get_bytes_per_block(transcoder_texture_format fmt);
  80. const char* basis_get_format_name(transcoder_texture_format fmt);
  81. bool basis_transcoder_format_has_alpha(transcoder_texture_format fmt);
  82. basisu::texture_format basis_get_basisu_texture_format(transcoder_texture_format fmt);
  83. const char* basis_get_texture_type_name(basis_texture_type tex_type);
  84. bool basis_transcoder_format_is_uncompressed(transcoder_texture_format tex_type);
  85. uint32_t basis_get_uncompressed_bytes_per_pixel(transcoder_texture_format fmt);
  86. uint32_t basis_get_block_width(transcoder_texture_format tex_type);
  87. uint32_t basis_get_block_height(transcoder_texture_format tex_type);
  88. // Returns true if the specified format was enabled at compile time.
  89. bool basis_is_format_supported(transcoder_texture_format tex_type);
  90. class basisu_transcoder;
  91. // This struct holds all state used during transcoding. For video, it needs to persist between image transcodes (it holds the previous frame).
  92. // For threading you can use one state per thread.
  93. struct basisu_transcoder_state
  94. {
  95. struct block_preds
  96. {
  97. uint16_t m_endpoint_index;
  98. uint8_t m_pred_bits;
  99. };
  100. std::vector<block_preds> m_block_endpoint_preds[2];
  101. enum { cMaxPrevFrameLevels = 16 };
  102. std::vector<uint32_t> m_prev_frame_indices[2][cMaxPrevFrameLevels]; // [alpha_flag][level_index]
  103. };
  104. // Low-level helper class that does the actual transcoding.
  105. class basisu_lowlevel_transcoder
  106. {
  107. friend class basisu_transcoder;
  108. public:
  109. basisu_lowlevel_transcoder(const basist::etc1_global_selector_codebook *pGlobal_sel_codebook);
  110. bool decode_palettes(
  111. uint32_t num_endpoints, const uint8_t *pEndpoints_data, uint32_t endpoints_data_size,
  112. uint32_t num_selectors, const uint8_t *pSelectors_data, uint32_t selectors_data_size);
  113. bool decode_tables(const uint8_t *pTable_data, uint32_t table_data_size);
  114. 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,
  115. 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,
  116. basisu_transcoder_state *pState = nullptr, bool astc_transcode_alpha = false, void* pAlpha_blocks = nullptr, uint32_t output_rows_in_pixels = 0);
  117. private:
  118. typedef std::vector<endpoint> endpoint_vec;
  119. endpoint_vec m_endpoints;
  120. typedef std::vector<selector> selector_vec;
  121. selector_vec m_selectors;
  122. const etc1_global_selector_codebook *m_pGlobal_sel_codebook;
  123. huffman_decoding_table m_endpoint_pred_model, m_delta_endpoint_model, m_selector_model, m_selector_history_buf_rle_model;
  124. uint32_t m_selector_history_buf_size;
  125. basisu_transcoder_state m_def_state;
  126. };
  127. struct basisu_slice_info
  128. {
  129. uint32_t m_orig_width;
  130. uint32_t m_orig_height;
  131. uint32_t m_width;
  132. uint32_t m_height;
  133. uint32_t m_num_blocks_x;
  134. uint32_t m_num_blocks_y;
  135. uint32_t m_total_blocks;
  136. uint32_t m_compressed_size;
  137. uint32_t m_slice_index; // the slice index in the .basis file
  138. uint32_t m_image_index; // the source image index originally provided to the encoder
  139. uint32_t m_level_index; // the mipmap level within this image
  140. uint32_t m_unpacked_slice_crc16;
  141. bool m_alpha_flag; // true if the slice has alpha data
  142. bool m_iframe_flag; // true if the slice is an I-Frame
  143. };
  144. typedef std::vector<basisu_slice_info> basisu_slice_info_vec;
  145. struct basisu_image_info
  146. {
  147. uint32_t m_image_index;
  148. uint32_t m_total_levels;
  149. uint32_t m_orig_width;
  150. uint32_t m_orig_height;
  151. uint32_t m_width;
  152. uint32_t m_height;
  153. uint32_t m_num_blocks_x;
  154. uint32_t m_num_blocks_y;
  155. uint32_t m_total_blocks;
  156. uint32_t m_first_slice_index;
  157. bool m_alpha_flag; // true if the image has alpha data
  158. bool m_iframe_flag; // true if the image is an I-Frame
  159. };
  160. struct basisu_image_level_info
  161. {
  162. uint32_t m_image_index;
  163. uint32_t m_level_index;
  164. uint32_t m_orig_width;
  165. uint32_t m_orig_height;
  166. uint32_t m_width;
  167. uint32_t m_height;
  168. uint32_t m_num_blocks_x;
  169. uint32_t m_num_blocks_y;
  170. uint32_t m_total_blocks;
  171. uint32_t m_first_slice_index;
  172. bool m_alpha_flag; // true if the image has alpha data
  173. bool m_iframe_flag; // true if the image is an I-Frame
  174. };
  175. struct basisu_file_info
  176. {
  177. uint32_t m_version;
  178. uint32_t m_total_header_size;
  179. uint32_t m_total_selectors;
  180. uint32_t m_selector_codebook_size;
  181. uint32_t m_total_endpoints;
  182. uint32_t m_endpoint_codebook_size;
  183. uint32_t m_tables_size;
  184. uint32_t m_slices_size;
  185. basis_texture_type m_tex_type;
  186. uint32_t m_us_per_frame;
  187. // Low-level slice information (1 slice per image for color-only basis files, 2 for alpha basis files)
  188. basisu_slice_info_vec m_slice_info;
  189. uint32_t m_total_images; // total # of images
  190. std::vector<uint32_t> m_image_mipmap_levels; // the # of mipmap levels for each image
  191. uint32_t m_userdata0;
  192. uint32_t m_userdata1;
  193. bool m_etc1s; // always true for basis universal
  194. bool m_y_flipped; // true if the image was Y flipped
  195. bool m_has_alpha_slices; // true if the texture has alpha slices (even slices RGB, odd slices alpha)
  196. };
  197. // High-level transcoder class which accepts .basis file data and allows the caller to query information about the file and transcode image levels to various texture formats.
  198. // If you're just starting out this is the class you care about.
  199. class basisu_transcoder
  200. {
  201. basisu_transcoder(basisu_transcoder&);
  202. basisu_transcoder& operator= (const basisu_transcoder&);
  203. public:
  204. basisu_transcoder(const etc1_global_selector_codebook *pGlobal_sel_codebook);
  205. // Validates the .basis file. This computes a crc16 over the entire file, so it's slow.
  206. bool validate_file_checksums(const void *pData, uint32_t data_size, bool full_validation) const;
  207. // Quick header validation - no crc16 checks.
  208. bool validate_header(const void *pData, uint32_t data_size) const;
  209. basis_texture_type get_texture_type(const void *pData, uint32_t data_size) const;
  210. bool get_userdata(const void *pData, uint32_t data_size, uint32_t &userdata0, uint32_t &userdata1) const;
  211. // Returns the total number of images in the basis file (always 1 or more).
  212. // Note that the number of mipmap levels for each image may differ, and that images may have different resolutions.
  213. uint32_t get_total_images(const void *pData, uint32_t data_size) const;
  214. // Returns the number of mipmap levels in an image.
  215. uint32_t get_total_image_levels(const void *pData, uint32_t data_size, uint32_t image_index) const;
  216. // Returns basic information about an image. Note that orig_width/orig_height may not be a multiple of 4.
  217. 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;
  218. // Returns information about the specified image.
  219. bool get_image_info(const void *pData, uint32_t data_size, basisu_image_info &image_info, uint32_t image_index) const;
  220. // Returns information about the specified image's mipmap level.
  221. 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;
  222. // Get a description of the basis file and low-level information about each slice.
  223. bool get_file_info(const void *pData, uint32_t data_size, basisu_file_info &file_info) const;
  224. // start_transcoding() must be called before calling transcode_slice() or transcode_image_level().
  225. // This decompresses the selector/endpoint codebooks, so ideally you would only call this once per .basis file (not each image/mipmap level).
  226. bool start_transcoding(const void *pData, uint32_t data_size) const;
  227. // Returns true if start_transcoding() has been called.
  228. bool get_ready_to_transcode() const { return m_lowlevel_decoder.m_endpoints.size() > 0; }
  229. enum
  230. {
  231. // PVRTC1: decode non-pow2 ETC1S texture level to the next larger power of 2 (not implemented yet, but we're going to support it). Ignored if the slice's dimensions are already a power of 2.
  232. cDecodeFlagsPVRTCDecodeToNextPow2 = 2,
  233. // When decoding to an opaque texture format, if the basis file has alpha, decode the alpha slice instead of the color slice to the output texture format.
  234. // This is primarily to allow decoding of textures with alpha to multiple ETC1 textures (one for color, another for alpha).
  235. cDecodeFlagsTranscodeAlphaDataToOpaqueFormats = 4,
  236. // Forbid usage of BC1 3 color blocks (we don't support BC1 punchthrough alpha yet).
  237. // This flag is used internally when decoding to BC3.
  238. cDecodeFlagsBC1ForbidThreeColorBlocks = 8,
  239. // The output buffer contains alpha endpoint/selector indices.
  240. // Used internally when decoding formats like ASTC that require both color and alpha data to be available when transcoding to the output format.
  241. cDecodeFlagsOutputHasAlphaIndices = 16
  242. };
  243. // transcode_image_level() decodes a single mipmap level from the .basis file to any of the supported output texture formats.
  244. // It'll first find the slice(s) to transcode, then call transcode_slice() one or two times to decode both the color and alpha texture data (or RG texture data from two slices for BC5).
  245. // If the .basis file doesn't have alpha slices, the output alpha blocks will be set to fully opaque (all 255's).
  246. // Currently, to decode to PVRTC1 the basis texture's dimensions in pixels must be a power of 2, due to PVRTC1 format requirements.
  247. // output_blocks_buf_size_in_blocks_or_pixels should be at least the image level's total_blocks (num_blocks_x * num_blocks_y), or the total number of output pixels if fmt==cTFRGBA32.
  248. // output_row_pitch_in_blocks_or_pixels: Number of blocks or pixels per row. If 0, the transcoder uses the slice's num_blocks_x or orig_width (NOT num_blocks_x * 4). Ignored for PVRTC1 (due to texture swizzling).
  249. // output_rows_in_pixels: Ignored unless fmt is cRGBA32. The total number of output rows in the output buffer. If 0, the transcoder assumes the slice's orig_height (NOT num_blocks_y * 4).
  250. // Notes:
  251. // - basisu_transcoder_init() must have been called first to initialize the transcoder lookup tables before calling this function.
  252. // - This method assumes the output texture buffer is readable. In some cases to handle alpha, the transcoder will write temporary data to the output texture in
  253. // a first pass, which will be read in a second pass.
  254. bool transcode_image_level(
  255. const void *pData, uint32_t data_size,
  256. uint32_t image_index, uint32_t level_index,
  257. void *pOutput_blocks, uint32_t output_blocks_buf_size_in_blocks_or_pixels,
  258. transcoder_texture_format fmt,
  259. 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;
  260. // Finds the basis slice corresponding to the specified image/level/alpha params, or -1 if the slice can't be found.
  261. int find_slice(const void *pData, uint32_t data_size, uint32_t image_index, uint32_t level_index, bool alpha_data) const;
  262. // transcode_slice() decodes a single slice from the .basis file. It's a low-level API - most likely you want to use transcode_image_level().
  263. // This is a low-level API, and will be needed to be called multiple times to decode some texture formats (like BC3, BC5, or ETC2).
  264. // output_blocks_buf_size_in_blocks_or_pixels is just used for verification to make sure the output buffer is large enough.
  265. // output_blocks_buf_size_in_blocks_or_pixels should be at least the image level's total_blocks (num_blocks_x * num_blocks_y), or the total number of output pixels if fmt==cTFRGBA32.
  266. // output_block_stride_in_bytes: Number of bytes between each output block.
  267. // output_row_pitch_in_blocks_or_pixels: Number of blocks or pixels per row. If 0, the transcoder uses the slice's num_blocks_x or orig_width (NOT num_blocks_x * 4). Ignored for PVRTC1 (due to texture swizzling).
  268. // output_rows_in_pixels: Ignored unless fmt is cRGBA32. The total number of output rows in the output buffer. If 0, the transcoder assumes the slice's orig_height (NOT num_blocks_y * 4).
  269. // Notes:
  270. // - basisu_transcoder_init() must have been called first to initialize the transcoder lookup tables before calling this function.
  271. bool transcode_slice(const void *pData, uint32_t data_size, uint32_t slice_index,
  272. void *pOutput_blocks, uint32_t output_blocks_buf_size_in_blocks_or_pixels,
  273. 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, uint32_t output_rows_in_pixels = 0) const;
  274. private:
  275. mutable basisu_lowlevel_transcoder m_lowlevel_decoder;
  276. int find_first_slice_index(const void* pData, uint32_t data_size, uint32_t image_index, uint32_t level_index) const;
  277. bool validate_header_quick(const void* pData, uint32_t data_size) const;
  278. };
  279. // basisu_transcoder_init() must be called before a .basis file can be transcoded.
  280. void basisu_transcoder_init();
  281. enum debug_flags_t
  282. {
  283. cDebugFlagVisCRs = 1,
  284. cDebugFlagVisBC1Sels = 2,
  285. cDebugFlagVisBC1Endpoints = 4
  286. };
  287. uint32_t get_debug_flags();
  288. void set_debug_flags(uint32_t f);
  289. } // namespace basisu