basisu_file_headers.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // basis_file_headers.h
  2. // Copyright (C) 2019-2020 Binomial LLC. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #pragma once
  16. #include "basisu_transcoder_internal.h"
  17. namespace basist
  18. {
  19. // Slice desc header flags
  20. enum basis_slice_desc_flags
  21. {
  22. cSliceDescFlagsHasAlpha = 1,
  23. // Video only: Frame doesn't refer to previous frame (no usage of conditional replenishment pred symbols)
  24. // Currently the first frame is always an I-Frame, all subsequent frames are P-Frames. This will eventually be changed to periodic I-Frames.
  25. cSliceDescFlagsFrameIsIFrame = 2
  26. };
  27. #pragma pack(push)
  28. #pragma pack(1)
  29. struct basis_slice_desc
  30. {
  31. basisu::packed_uint<3> m_image_index; // The index of the source image provided to the encoder (will always appear in order from first to last, first image index is 0, no skipping allowed)
  32. basisu::packed_uint<1> m_level_index; // The mipmap level index (mipmaps will always appear from largest to smallest)
  33. basisu::packed_uint<1> m_flags; // enum basis_slice_desc_flags
  34. basisu::packed_uint<2> m_orig_width; // The original image width (may not be a multiple of 4 pixels)
  35. basisu::packed_uint<2> m_orig_height; // The original image height (may not be a multiple of 4 pixels)
  36. basisu::packed_uint<2> m_num_blocks_x; // The slice's block X dimensions. Each block is 4x4 pixels. The slice's pixel resolution may or may not be a power of 2.
  37. basisu::packed_uint<2> m_num_blocks_y; // The slice's block Y dimensions.
  38. basisu::packed_uint<4> m_file_ofs; // Offset from the start of the file to the start of the slice's data
  39. basisu::packed_uint<4> m_file_size; // The size of the compressed slice data in bytes
  40. basisu::packed_uint<2> m_slice_data_crc16; // The CRC16 of the compressed slice data, for extra-paranoid use cases
  41. };
  42. // File header files
  43. enum basis_header_flags
  44. {
  45. // Always set for ETC1S files. Not set for UASTC files.
  46. cBASISHeaderFlagETC1S = 1,
  47. // Set if the texture had to be Y flipped before encoding. The actual interpretation of this (is Y up or down?) is up to the user.
  48. cBASISHeaderFlagYFlipped = 2,
  49. // Set if any slices contain alpha (for ETC1S, if the odd slices contain alpha data)
  50. cBASISHeaderFlagHasAlphaSlices = 4,
  51. // For ETC1S files, this will be true if the file utilizes a codebook from another .basis file.
  52. cBASISHeaderFlagUsesGlobalCodebook = 8,
  53. // Set if the texture data is sRGB, otherwise it's linear.
  54. // In reality, we have no idea if the texture data is actually linear or sRGB. This is the m_perceptual parameter passed to the compressor.
  55. cBASISHeaderFlagSRGB = 16,
  56. };
  57. // The image type field attempts to describe how to interpret the image data in a Basis file.
  58. // The encoder library doesn't really do anything special or different with these texture types, this is mostly here for the benefit of the user.
  59. // We do make sure the various constraints are followed (2DArray/cubemap/videoframes/volume implies that each image has the same resolution and # of mipmap levels, etc., cubemap implies that the # of image slices is a multiple of 6)
  60. enum basis_texture_type
  61. {
  62. cBASISTexType2D = 0, // An arbitrary array of 2D RGB or RGBA images with optional mipmaps, array size = # images, each image may have a different resolution and # of mipmap levels
  63. cBASISTexType2DArray = 1, // An array of 2D RGB or RGBA images with optional mipmaps, array size = # images, each image has the same resolution and mipmap levels
  64. cBASISTexTypeCubemapArray = 2, // an array of cubemap levels, total # of images must be divisable by 6, in X+, X-, Y+, Y-, Z+, Z- order, with optional mipmaps
  65. cBASISTexTypeVideoFrames = 3, // An array of 2D video frames, with optional mipmaps, # frames = # images, each image has the same resolution and # of mipmap levels
  66. cBASISTexTypeVolume = 4, // A 3D texture with optional mipmaps, Z dimension = # images, each image has the same resolution and # of mipmap levels
  67. cBASISTexTypeTotal
  68. };
  69. enum
  70. {
  71. cBASISMaxUSPerFrame = 0xFFFFFF
  72. };
  73. enum class basis_tex_format
  74. {
  75. cETC1S = 0,
  76. cUASTC4x4 = 1
  77. };
  78. struct basis_file_header
  79. {
  80. enum
  81. {
  82. cBASISSigValue = ('B' << 8) | 's',
  83. cBASISFirstVersion = 0x10
  84. };
  85. basisu::packed_uint<2> m_sig; // 2 byte file signature
  86. basisu::packed_uint<2> m_ver; // Baseline file version
  87. basisu::packed_uint<2> m_header_size; // Header size in bytes, sizeof(basis_file_header)
  88. basisu::packed_uint<2> m_header_crc16; // CRC16 of the remaining header data
  89. basisu::packed_uint<4> m_data_size; // The total size of all data after the header
  90. basisu::packed_uint<2> m_data_crc16; // The CRC16 of all data after the header
  91. basisu::packed_uint<3> m_total_slices; // The total # of compressed slices (1 slice per image, or 2 for alpha .basis files)
  92. basisu::packed_uint<3> m_total_images; // The total # of images
  93. basisu::packed_uint<1> m_tex_format; // enum basis_tex_format
  94. basisu::packed_uint<2> m_flags; // enum basist::header_flags
  95. basisu::packed_uint<1> m_tex_type; // enum basist::basis_texture_type
  96. basisu::packed_uint<3> m_us_per_frame; // Framerate of video, in microseconds per frame
  97. basisu::packed_uint<4> m_reserved; // For future use
  98. basisu::packed_uint<4> m_userdata0; // For client use
  99. basisu::packed_uint<4> m_userdata1; // For client use
  100. basisu::packed_uint<2> m_total_endpoints; // The number of endpoints in the endpoint codebook
  101. basisu::packed_uint<4> m_endpoint_cb_file_ofs; // The compressed endpoint codebook's file offset relative to the start of the file
  102. basisu::packed_uint<3> m_endpoint_cb_file_size; // The compressed endpoint codebook's size in bytes
  103. basisu::packed_uint<2> m_total_selectors; // The number of selectors in the endpoint codebook
  104. basisu::packed_uint<4> m_selector_cb_file_ofs; // The compressed selectors codebook's file offset relative to the start of the file
  105. basisu::packed_uint<3> m_selector_cb_file_size; // The compressed selector codebook's size in bytes
  106. basisu::packed_uint<4> m_tables_file_ofs; // The file offset of the compressed Huffman codelength tables, for decompressing slices
  107. basisu::packed_uint<4> m_tables_file_size; // The file size in bytes of the compressed huffman codelength tables
  108. basisu::packed_uint<4> m_slice_desc_file_ofs; // The file offset to the slice description array, usually follows the header
  109. basisu::packed_uint<4> m_extended_file_ofs; // The file offset of the "extended" header and compressed data, for future use
  110. basisu::packed_uint<4> m_extended_file_size; // The file size in bytes of the "extended" header and compressed data, for future use
  111. };
  112. #pragma pack (pop)
  113. } // namespace basist