basisu_backend.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // basisu_backend.h
  2. // Copyright (C) 2019-2021 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 "../transcoder/basisu.h"
  17. #include "basisu_enc.h"
  18. #include "../transcoder/basisu_transcoder_internal.h"
  19. #include "basisu_frontend.h"
  20. namespace basisu
  21. {
  22. struct etc1_selector_palette_entry
  23. {
  24. etc1_selector_palette_entry()
  25. {
  26. clear();
  27. }
  28. void clear()
  29. {
  30. basisu::clear_obj(*this);
  31. }
  32. uint8_t operator[] (uint32_t i) const { assert(i < 16); return m_selectors[i]; }
  33. uint8_t& operator[] (uint32_t i) { assert(i < 16); return m_selectors[i]; }
  34. void set_uint32(uint32_t v)
  35. {
  36. for (uint32_t byte_index = 0; byte_index < 4; byte_index++)
  37. {
  38. uint32_t b = (v >> (byte_index * 8)) & 0xFF;
  39. m_selectors[byte_index * 4 + 0] = b & 3;
  40. m_selectors[byte_index * 4 + 1] = (b >> 2) & 3;
  41. m_selectors[byte_index * 4 + 2] = (b >> 4) & 3;
  42. m_selectors[byte_index * 4 + 3] = (b >> 6) & 3;
  43. }
  44. }
  45. uint32_t get_uint32() const
  46. {
  47. return get_byte(0) | (get_byte(1) << 8) | (get_byte(2) << 16) | (get_byte(3) << 24);
  48. }
  49. uint32_t get_byte(uint32_t byte_index) const
  50. {
  51. assert(byte_index < 4);
  52. return m_selectors[byte_index * 4 + 0] |
  53. (m_selectors[byte_index * 4 + 1] << 2) |
  54. (m_selectors[byte_index * 4 + 2] << 4) |
  55. (m_selectors[byte_index * 4 + 3] << 6);
  56. }
  57. uint8_t operator()(uint32_t x, uint32_t y) const { assert((x < 4) && (y < 4)); return m_selectors[x + y * 4]; }
  58. uint8_t& operator()(uint32_t x, uint32_t y) { assert((x < 4) && (y < 4)); return m_selectors[x + y * 4]; }
  59. bool operator< (const etc1_selector_palette_entry& other) const
  60. {
  61. for (uint32_t i = 0; i < 16; i++)
  62. {
  63. if (m_selectors[i] < other.m_selectors[i])
  64. return true;
  65. else if (m_selectors[i] != other.m_selectors[i])
  66. return false;
  67. }
  68. return false;
  69. }
  70. bool operator== (const etc1_selector_palette_entry& other) const
  71. {
  72. for (uint32_t i = 0; i < 16; i++)
  73. {
  74. if (m_selectors[i] != other.m_selectors[i])
  75. return false;
  76. }
  77. return true;
  78. }
  79. private:
  80. uint8_t m_selectors[16];
  81. };
  82. typedef basisu::vector<etc1_selector_palette_entry> etc1_selector_palette_entry_vec;
  83. struct encoder_block
  84. {
  85. encoder_block()
  86. {
  87. clear();
  88. }
  89. uint32_t m_endpoint_predictor;
  90. int m_endpoint_index;
  91. int m_selector_index;
  92. int m_selector_history_buf_index;
  93. bool m_is_cr_target;
  94. void clear()
  95. {
  96. m_endpoint_predictor = 0;
  97. m_endpoint_index = 0;
  98. m_selector_index = 0;
  99. m_selector_history_buf_index = 0;
  100. m_is_cr_target = false;
  101. }
  102. };
  103. typedef basisu::vector<encoder_block> encoder_block_vec;
  104. typedef vector2D<encoder_block> encoder_block_vec2D;
  105. struct etc1_endpoint_palette_entry
  106. {
  107. etc1_endpoint_palette_entry()
  108. {
  109. clear();
  110. }
  111. color_rgba m_color5;
  112. uint32_t m_inten5;
  113. bool m_color5_valid;
  114. void clear()
  115. {
  116. clear_obj(*this);
  117. }
  118. };
  119. typedef basisu::vector<etc1_endpoint_palette_entry> etc1_endpoint_palette_entry_vec;
  120. struct basisu_backend_params
  121. {
  122. bool m_etc1s;
  123. bool m_debug, m_debug_images;
  124. float m_endpoint_rdo_quality_thresh;
  125. float m_selector_rdo_quality_thresh;
  126. uint32_t m_compression_level;
  127. bool m_used_global_codebooks;
  128. bool m_validate;
  129. basisu_backend_params()
  130. {
  131. clear();
  132. }
  133. void clear()
  134. {
  135. m_etc1s = false;
  136. m_debug = false;
  137. m_debug_images = false;
  138. m_endpoint_rdo_quality_thresh = 0.0f;
  139. m_selector_rdo_quality_thresh = 0.0f;
  140. m_compression_level = 0;
  141. m_used_global_codebooks = false;
  142. m_validate = true;
  143. }
  144. };
  145. struct basisu_backend_slice_desc
  146. {
  147. basisu_backend_slice_desc()
  148. {
  149. clear();
  150. }
  151. void clear()
  152. {
  153. clear_obj(*this);
  154. }
  155. uint32_t m_first_block_index;
  156. uint32_t m_orig_width;
  157. uint32_t m_orig_height;
  158. uint32_t m_width;
  159. uint32_t m_height;
  160. uint32_t m_num_blocks_x;
  161. uint32_t m_num_blocks_y;
  162. uint32_t m_num_macroblocks_x;
  163. uint32_t m_num_macroblocks_y;
  164. uint32_t m_source_file_index; // also the basis image index
  165. uint32_t m_mip_index;
  166. bool m_alpha;
  167. bool m_iframe;
  168. };
  169. typedef basisu::vector<basisu_backend_slice_desc> basisu_backend_slice_desc_vec;
  170. struct basisu_backend_output
  171. {
  172. basist::basis_tex_format m_tex_format;
  173. bool m_etc1s;
  174. bool m_uses_global_codebooks;
  175. bool m_srgb;
  176. uint32_t m_num_endpoints;
  177. uint32_t m_num_selectors;
  178. uint8_vec m_endpoint_palette;
  179. uint8_vec m_selector_palette;
  180. basisu_backend_slice_desc_vec m_slice_desc;
  181. uint8_vec m_slice_image_tables;
  182. basisu::vector<uint8_vec> m_slice_image_data;
  183. uint16_vec m_slice_image_crcs;
  184. basisu_backend_output()
  185. {
  186. clear();
  187. }
  188. void clear()
  189. {
  190. m_tex_format = basist::basis_tex_format::cETC1S;
  191. m_etc1s = false;
  192. m_uses_global_codebooks = false;
  193. m_srgb = true;
  194. m_num_endpoints = 0;
  195. m_num_selectors = 0;
  196. m_endpoint_palette.clear();
  197. m_selector_palette.clear();
  198. m_slice_desc.clear();
  199. m_slice_image_tables.clear();
  200. m_slice_image_data.clear();
  201. m_slice_image_crcs.clear();
  202. }
  203. uint32_t get_output_size_estimate() const
  204. {
  205. uint32_t total_compressed_bytes = (uint32_t)(m_slice_image_tables.size() + m_endpoint_palette.size() + m_selector_palette.size());
  206. for (uint32_t i = 0; i < m_slice_image_data.size(); i++)
  207. total_compressed_bytes += (uint32_t)m_slice_image_data[i].size();
  208. return total_compressed_bytes;
  209. }
  210. };
  211. class basisu_backend
  212. {
  213. BASISU_NO_EQUALS_OR_COPY_CONSTRUCT(basisu_backend);
  214. public:
  215. basisu_backend();
  216. void clear();
  217. void init(basisu_frontend *pFront_end, basisu_backend_params &params, const basisu_backend_slice_desc_vec &slice_desc);
  218. uint32_t encode();
  219. const basisu_backend_output &get_output() const { return m_output; }
  220. const basisu_backend_params& get_params() const { return m_params; }
  221. private:
  222. basisu_frontend *m_pFront_end;
  223. basisu_backend_params m_params;
  224. basisu_backend_slice_desc_vec m_slices;
  225. basisu_backend_output m_output;
  226. etc1_endpoint_palette_entry_vec m_endpoint_palette;
  227. etc1_selector_palette_entry_vec m_selector_palette;
  228. struct etc1_global_selector_cb_entry_desc
  229. {
  230. uint32_t m_pal_index;
  231. uint32_t m_mod_index;
  232. bool m_was_used;
  233. };
  234. typedef basisu::vector<etc1_global_selector_cb_entry_desc> etc1_global_selector_cb_entry_desc_vec;
  235. etc1_global_selector_cb_entry_desc_vec m_global_selector_palette_desc;
  236. basisu::vector<encoder_block_vec2D> m_slice_encoder_blocks;
  237. // Maps OLD to NEW endpoint/selector indices
  238. uint_vec m_endpoint_remap_table_old_to_new;
  239. uint_vec m_endpoint_remap_table_new_to_old;
  240. bool_vec m_old_endpoint_was_used;
  241. bool_vec m_new_endpoint_was_used;
  242. uint_vec m_selector_remap_table_old_to_new;
  243. // Maps NEW to OLD endpoint/selector indices
  244. uint_vec m_selector_remap_table_new_to_old;
  245. uint32_t get_total_slices() const
  246. {
  247. return (uint32_t)m_slices.size();
  248. }
  249. uint32_t get_total_slice_blocks() const
  250. {
  251. return m_pFront_end->get_total_output_blocks();
  252. }
  253. uint32_t get_block_index(uint32_t slice_index, uint32_t block_x, uint32_t block_y) const
  254. {
  255. const basisu_backend_slice_desc &slice = m_slices[slice_index];
  256. assert((block_x < slice.m_num_blocks_x) && (block_y < slice.m_num_blocks_y));
  257. return slice.m_first_block_index + block_y * slice.m_num_blocks_x + block_x;
  258. }
  259. uint32_t get_total_blocks(uint32_t slice_index) const
  260. {
  261. return m_slices[slice_index].m_num_blocks_x * m_slices[slice_index].m_num_blocks_y;
  262. }
  263. uint32_t get_total_blocks() const
  264. {
  265. uint32_t total_blocks = 0;
  266. for (uint32_t i = 0; i < m_slices.size(); i++)
  267. total_blocks += get_total_blocks(i);
  268. return total_blocks;
  269. }
  270. // Returns the total number of input texels, not counting padding up to blocks/macroblocks.
  271. uint32_t get_total_input_texels(uint32_t slice_index) const
  272. {
  273. return m_slices[slice_index].m_orig_width * m_slices[slice_index].m_orig_height;
  274. }
  275. uint32_t get_total_input_texels() const
  276. {
  277. uint32_t total_texels = 0;
  278. for (uint32_t i = 0; i < m_slices.size(); i++)
  279. total_texels += get_total_input_texels(i);
  280. return total_texels;
  281. }
  282. int find_slice(uint32_t block_index, uint32_t *pBlock_x, uint32_t *pBlock_y) const
  283. {
  284. for (uint32_t i = 0; i < m_slices.size(); i++)
  285. {
  286. if ((block_index >= m_slices[i].m_first_block_index) && (block_index < (m_slices[i].m_first_block_index + m_slices[i].m_num_blocks_x * m_slices[i].m_num_blocks_y)))
  287. {
  288. const uint32_t ofs = block_index - m_slices[i].m_first_block_index;
  289. const uint32_t x = ofs % m_slices[i].m_num_blocks_x;
  290. const uint32_t y = ofs / m_slices[i].m_num_blocks_x;
  291. if (pBlock_x) *pBlock_x = x;
  292. if (pBlock_y) *pBlock_y = y;
  293. return i;
  294. }
  295. }
  296. return -1;
  297. }
  298. void create_endpoint_palette();
  299. void create_selector_palette();
  300. // endpoint palette
  301. // 5:5:5 and predicted 4:4:4 colors, 1 or 2 3-bit intensity table indices
  302. // selector palette
  303. // 4x4 2-bit selectors
  304. // per-macroblock:
  305. // 4 diff bits
  306. // 4 flip bits
  307. // Endpoint template index, 1-8 endpoint indices
  308. // Alternately, if no template applies, we can send 4 ETC1S bits followed by 4-8 endpoint indices
  309. // 4 selector indices
  310. void reoptimize_and_sort_endpoints_codebook(uint32_t total_block_endpoints_remapped, uint_vec &all_endpoint_indices);
  311. void sort_selector_codebook();
  312. void create_encoder_blocks();
  313. void compute_slice_crcs();
  314. bool encode_image();
  315. bool encode_endpoint_palette();
  316. bool encode_selector_palette();
  317. int find_video_frame(int slice_index, int delta);
  318. void check_for_valid_cr_blocks();
  319. };
  320. } // namespace basisu