basisu_transcoder_internal.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. // basisu_transcoder_internal.h - Universal texture format transcoder library.
  2. // Copyright (C) 2019 Binomial LLC. All Rights Reserved.
  3. //
  4. // Important: If compiling with gcc, be sure strict aliasing is disabled: -fno-strict-aliasing
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. #pragma once
  18. #ifdef _MSC_VER
  19. #pragma warning (disable: 4127) // conditional expression is constant
  20. #endif
  21. #define BASISD_LIB_VERSION 107
  22. #define BASISD_VERSION_STRING "01.11"
  23. #ifdef _DEBUG
  24. #define BASISD_BUILD_DEBUG
  25. #else
  26. #define BASISD_BUILD_RELEASE
  27. #endif
  28. #include "basisu.h"
  29. #define BASISD_znew (z = 36969 * (z & 65535) + (z >> 16))
  30. namespace basisu
  31. {
  32. extern bool g_debug_printf;
  33. }
  34. namespace basist
  35. {
  36. // Low-level formats directly supported by the transcoder (other supported texture formats are combinations of these low-level block formats).
  37. // You probably don't care about these enum's unless you are going pretty low-level and calling the transcoder to decode individual slices.
  38. enum block_format
  39. {
  40. cETC1, // ETC1S RGB
  41. cBC1, // DXT1 RGB
  42. cBC4, // DXT5A (alpha block only)
  43. cPVRTC1_4_RGB, // opaque-only PVRTC1 4bpp
  44. cPVRTC1_4_RGBA, // PVRTC1 4bpp RGBA
  45. cBC7_M6_OPAQUE_ONLY, // RGB BC7 mode 6
  46. cBC7_M5_COLOR, // RGB BC7 mode 5 color (writes an opaque mode 5 block)
  47. cBC7_M5_ALPHA, // alpha portion of BC7 mode 5 (cBC7_M5_COLOR output data must have been written to the output buffer first to set the mode/rot fields etc.)
  48. cETC2_EAC_A8, // alpha block of ETC2 EAC (first 8 bytes of the 16-bit ETC2 EAC RGBA format)
  49. cASTC_4x4, // ASTC 4x4 (either color-only or color+alpha). Note that the transcoder always currently assumes sRGB is not enabled when outputting ASTC
  50. // data. If you use a sRGB ASTC format you'll get ~1 LSB of additional error, because of the different way ASTC decoders scale 8-bit endpoints to 16-bits during unpacking.
  51. cATC_RGB,
  52. cATC_RGBA_INTERPOLATED_ALPHA,
  53. cIndices, // Used internally: Write 16-bit endpoint and selector indices directly to output (output block must be at least 32-bits)
  54. cRGB32, // Writes RGB components to 32bpp output pixels
  55. cRGBA32, // Writes RGB255 components to 32bpp output pixels
  56. cA32, // Writes alpha component to 32bpp output pixels
  57. cRGB565,
  58. cBGR565,
  59. cRGBA4444_COLOR,
  60. cRGBA4444_ALPHA,
  61. cRGBA4444_COLOR_OPAQUE,
  62. cTotalBlockFormats
  63. };
  64. const int COLOR5_PAL0_PREV_HI = 9, COLOR5_PAL0_DELTA_LO = -9, COLOR5_PAL0_DELTA_HI = 31;
  65. const int COLOR5_PAL1_PREV_HI = 21, COLOR5_PAL1_DELTA_LO = -21, COLOR5_PAL1_DELTA_HI = 21;
  66. const int COLOR5_PAL2_PREV_HI = 31, COLOR5_PAL2_DELTA_LO = -31, COLOR5_PAL2_DELTA_HI = 9;
  67. const int COLOR5_PAL_MIN_DELTA_B_RUNLEN = 3, COLOR5_PAL_DELTA_5_RUNLEN_VLC_BITS = 3;
  68. const uint32_t ENDPOINT_PRED_TOTAL_SYMBOLS = (4 * 4 * 4 * 4) + 1;
  69. const uint32_t ENDPOINT_PRED_REPEAT_LAST_SYMBOL = ENDPOINT_PRED_TOTAL_SYMBOLS - 1;
  70. const uint32_t ENDPOINT_PRED_MIN_REPEAT_COUNT = 3;
  71. const uint32_t ENDPOINT_PRED_COUNT_VLC_BITS = 4;
  72. const uint32_t NUM_ENDPOINT_PREDS = 3;// BASISU_ARRAY_SIZE(g_endpoint_preds);
  73. const uint32_t CR_ENDPOINT_PRED_INDEX = NUM_ENDPOINT_PREDS - 1;
  74. const uint32_t NO_ENDPOINT_PRED_INDEX = 3;//NUM_ENDPOINT_PREDS;
  75. const uint32_t MAX_SELECTOR_HISTORY_BUF_SIZE = 64;
  76. const uint32_t SELECTOR_HISTORY_BUF_RLE_COUNT_THRESH = 3;
  77. const uint32_t SELECTOR_HISTORY_BUF_RLE_COUNT_BITS = 6;
  78. const uint32_t SELECTOR_HISTORY_BUF_RLE_COUNT_TOTAL = (1 << SELECTOR_HISTORY_BUF_RLE_COUNT_BITS);
  79. uint16_t crc16(const void *r, size_t size, uint16_t crc);
  80. class huffman_decoding_table
  81. {
  82. friend class bitwise_decoder;
  83. public:
  84. huffman_decoding_table()
  85. {
  86. }
  87. void clear()
  88. {
  89. basisu::clear_vector(m_code_sizes);
  90. basisu::clear_vector(m_lookup);
  91. basisu::clear_vector(m_tree);
  92. }
  93. bool init(uint32_t total_syms, const uint8_t *pCode_sizes)
  94. {
  95. if (!total_syms)
  96. {
  97. clear();
  98. return true;
  99. }
  100. m_code_sizes.resize(total_syms);
  101. memcpy(&m_code_sizes[0], pCode_sizes, total_syms);
  102. m_lookup.resize(0);
  103. m_lookup.resize(basisu::cHuffmanFastLookupSize);
  104. m_tree.resize(0);
  105. m_tree.resize(total_syms * 2);
  106. uint32_t syms_using_codesize[basisu::cHuffmanMaxSupportedInternalCodeSize + 1];
  107. basisu::clear_obj(syms_using_codesize);
  108. for (uint32_t i = 0; i < total_syms; i++)
  109. {
  110. if (pCode_sizes[i] > basisu::cHuffmanMaxSupportedInternalCodeSize)
  111. return false;
  112. syms_using_codesize[pCode_sizes[i]]++;
  113. }
  114. uint32_t next_code[basisu::cHuffmanMaxSupportedInternalCodeSize + 1];
  115. next_code[0] = next_code[1] = 0;
  116. uint32_t used_syms = 0, total = 0;
  117. for (uint32_t i = 1; i < basisu::cHuffmanMaxSupportedInternalCodeSize; i++)
  118. {
  119. used_syms += syms_using_codesize[i];
  120. next_code[i + 1] = (total = ((total + syms_using_codesize[i]) << 1));
  121. }
  122. if (((1U << basisu::cHuffmanMaxSupportedInternalCodeSize) != total) && (used_syms > 1U))
  123. return false;
  124. for (int tree_next = -1, sym_index = 0; sym_index < (int)total_syms; ++sym_index)
  125. {
  126. uint32_t rev_code = 0, l, cur_code, code_size = pCode_sizes[sym_index];
  127. if (!code_size)
  128. continue;
  129. cur_code = next_code[code_size]++;
  130. for (l = code_size; l > 0; l--, cur_code >>= 1)
  131. rev_code = (rev_code << 1) | (cur_code & 1);
  132. if (code_size <= basisu::cHuffmanFastLookupBits)
  133. {
  134. uint32_t k = (code_size << 16) | sym_index;
  135. while (rev_code < basisu::cHuffmanFastLookupSize)
  136. {
  137. if (m_lookup[rev_code] != 0)
  138. {
  139. // Supplied codesizes can't create a valid prefix code.
  140. return false;
  141. }
  142. m_lookup[rev_code] = k;
  143. rev_code += (1 << code_size);
  144. }
  145. continue;
  146. }
  147. int tree_cur;
  148. if (0 == (tree_cur = m_lookup[rev_code & (basisu::cHuffmanFastLookupSize - 1)]))
  149. {
  150. const uint32_t idx = rev_code & (basisu::cHuffmanFastLookupSize - 1);
  151. if (m_lookup[idx] != 0)
  152. {
  153. // Supplied codesizes can't create a valid prefix code.
  154. return false;
  155. }
  156. m_lookup[idx] = tree_next;
  157. tree_cur = tree_next;
  158. tree_next -= 2;
  159. }
  160. if (tree_cur >= 0)
  161. {
  162. // Supplied codesizes can't create a valid prefix code.
  163. return false;
  164. }
  165. rev_code >>= (basisu::cHuffmanFastLookupBits - 1);
  166. for (int j = code_size; j > (basisu::cHuffmanFastLookupBits + 1); j--)
  167. {
  168. tree_cur -= ((rev_code >>= 1) & 1);
  169. const int idx = -tree_cur - 1;
  170. if (idx < 0)
  171. return false;
  172. else if (idx >= (int)m_tree.size())
  173. m_tree.resize(idx + 1);
  174. if (!m_tree[idx])
  175. {
  176. m_tree[idx] = (int16_t)tree_next;
  177. tree_cur = tree_next;
  178. tree_next -= 2;
  179. }
  180. else
  181. {
  182. tree_cur = m_tree[idx];
  183. if (tree_cur >= 0)
  184. {
  185. // Supplied codesizes can't create a valid prefix code.
  186. return false;
  187. }
  188. }
  189. }
  190. tree_cur -= ((rev_code >>= 1) & 1);
  191. const int idx = -tree_cur - 1;
  192. if (idx < 0)
  193. return false;
  194. else if (idx >= (int)m_tree.size())
  195. m_tree.resize(idx + 1);
  196. if (m_tree[idx] != 0)
  197. {
  198. // Supplied codesizes can't create a valid prefix code.
  199. return false;
  200. }
  201. m_tree[idx] = (int16_t)sym_index;
  202. }
  203. return true;
  204. }
  205. const basisu::uint8_vec &get_code_sizes() const { return m_code_sizes; }
  206. bool is_valid() const { return m_code_sizes.size() > 0; }
  207. private:
  208. basisu::uint8_vec m_code_sizes;
  209. basisu::int_vec m_lookup;
  210. basisu::int16_vec m_tree;
  211. };
  212. class bitwise_decoder
  213. {
  214. public:
  215. bitwise_decoder() :
  216. m_buf_size(0),
  217. m_pBuf(nullptr),
  218. m_pBuf_start(nullptr),
  219. m_pBuf_end(nullptr),
  220. m_bit_buf(0),
  221. m_bit_buf_size(0)
  222. {
  223. }
  224. void clear()
  225. {
  226. m_buf_size = 0;
  227. m_pBuf = nullptr;
  228. m_pBuf_start = nullptr;
  229. m_pBuf_end = nullptr;
  230. m_bit_buf = 0;
  231. m_bit_buf_size = 0;
  232. }
  233. bool init(const uint8_t *pBuf, uint32_t buf_size)
  234. {
  235. if ((!pBuf) && (buf_size))
  236. return false;
  237. m_buf_size = buf_size;
  238. m_pBuf = pBuf;
  239. m_pBuf_start = pBuf;
  240. m_pBuf_end = pBuf + buf_size;
  241. m_bit_buf = 0;
  242. m_bit_buf_size = 0;
  243. return true;
  244. }
  245. void stop()
  246. {
  247. }
  248. inline uint32_t peek_bits(uint32_t num_bits)
  249. {
  250. if (!num_bits)
  251. return 0;
  252. assert(num_bits <= 25);
  253. while (m_bit_buf_size < num_bits)
  254. {
  255. uint32_t c = 0;
  256. if (m_pBuf < m_pBuf_end)
  257. c = *m_pBuf++;
  258. m_bit_buf |= (c << m_bit_buf_size);
  259. m_bit_buf_size += 8;
  260. assert(m_bit_buf_size <= 32);
  261. }
  262. return m_bit_buf & ((1 << num_bits) - 1);
  263. }
  264. void remove_bits(uint32_t num_bits)
  265. {
  266. assert(m_bit_buf_size >= num_bits);
  267. m_bit_buf >>= num_bits;
  268. m_bit_buf_size -= num_bits;
  269. }
  270. uint32_t get_bits(uint32_t num_bits)
  271. {
  272. if (num_bits > 25)
  273. {
  274. assert(num_bits <= 32);
  275. const uint32_t bits0 = peek_bits(25);
  276. m_bit_buf >>= 25;
  277. m_bit_buf_size -= 25;
  278. num_bits -= 25;
  279. const uint32_t bits = peek_bits(num_bits);
  280. m_bit_buf >>= num_bits;
  281. m_bit_buf_size -= num_bits;
  282. return bits0 | (bits << 25);
  283. }
  284. const uint32_t bits = peek_bits(num_bits);
  285. m_bit_buf >>= num_bits;
  286. m_bit_buf_size -= num_bits;
  287. return bits;
  288. }
  289. uint32_t decode_truncated_binary(uint32_t n)
  290. {
  291. assert(n >= 2);
  292. const uint32_t k = basisu::floor_log2i(n);
  293. const uint32_t u = (1 << (k + 1)) - n;
  294. uint32_t result = get_bits(k);
  295. if (result >= u)
  296. result = ((result << 1) | get_bits(1)) - u;
  297. return result;
  298. }
  299. uint32_t decode_rice(uint32_t m)
  300. {
  301. assert(m);
  302. uint32_t q = 0;
  303. for (;;)
  304. {
  305. uint32_t k = peek_bits(16);
  306. uint32_t l = 0;
  307. while (k & 1)
  308. {
  309. l++;
  310. k >>= 1;
  311. }
  312. q += l;
  313. remove_bits(l);
  314. if (l < 16)
  315. break;
  316. }
  317. return (q << m) + (get_bits(m + 1) >> 1);
  318. }
  319. inline uint32_t decode_vlc(uint32_t chunk_bits)
  320. {
  321. assert(chunk_bits);
  322. const uint32_t chunk_size = 1 << chunk_bits;
  323. const uint32_t chunk_mask = chunk_size - 1;
  324. uint32_t v = 0;
  325. uint32_t ofs = 0;
  326. for ( ; ; )
  327. {
  328. uint32_t s = get_bits(chunk_bits + 1);
  329. v |= ((s & chunk_mask) << ofs);
  330. ofs += chunk_bits;
  331. if ((s & chunk_size) == 0)
  332. break;
  333. if (ofs >= 32)
  334. {
  335. assert(0);
  336. break;
  337. }
  338. }
  339. return v;
  340. }
  341. inline uint32_t decode_huffman(const huffman_decoding_table &ct)
  342. {
  343. assert(ct.m_code_sizes.size());
  344. while (m_bit_buf_size < 16)
  345. {
  346. uint32_t c = 0;
  347. if (m_pBuf < m_pBuf_end)
  348. c = *m_pBuf++;
  349. m_bit_buf |= (c << m_bit_buf_size);
  350. m_bit_buf_size += 8;
  351. assert(m_bit_buf_size <= 32);
  352. }
  353. int code_len;
  354. int sym;
  355. if ((sym = ct.m_lookup[m_bit_buf & (basisu::cHuffmanFastLookupSize - 1)]) >= 0)
  356. {
  357. code_len = sym >> 16;
  358. sym &= 0xFFFF;
  359. }
  360. else
  361. {
  362. code_len = basisu::cHuffmanFastLookupBits;
  363. do
  364. {
  365. sym = ct.m_tree[~sym + ((m_bit_buf >> code_len++) & 1)]; // ~sym = -sym - 1
  366. } while (sym < 0);
  367. }
  368. m_bit_buf >>= code_len;
  369. m_bit_buf_size -= code_len;
  370. return sym;
  371. }
  372. bool read_huffman_table(huffman_decoding_table &ct)
  373. {
  374. ct.clear();
  375. const uint32_t total_used_syms = get_bits(basisu::cHuffmanMaxSymsLog2);
  376. if (!total_used_syms)
  377. return true;
  378. if (total_used_syms > basisu::cHuffmanMaxSyms)
  379. return false;
  380. uint8_t code_length_code_sizes[basisu::cHuffmanTotalCodelengthCodes];
  381. basisu::clear_obj(code_length_code_sizes);
  382. const uint32_t num_codelength_codes = get_bits(5);
  383. if ((num_codelength_codes < 1) || (num_codelength_codes > basisu::cHuffmanTotalCodelengthCodes))
  384. return false;
  385. for (uint32_t i = 0; i < num_codelength_codes; i++)
  386. code_length_code_sizes[basisu::g_huffman_sorted_codelength_codes[i]] = static_cast<uint8_t>(get_bits(3));
  387. huffman_decoding_table code_length_table;
  388. if (!code_length_table.init(basisu::cHuffmanTotalCodelengthCodes, code_length_code_sizes))
  389. return false;
  390. if (!code_length_table.is_valid())
  391. return false;
  392. basisu::uint8_vec code_sizes(total_used_syms);
  393. uint32_t cur = 0;
  394. while (cur < total_used_syms)
  395. {
  396. int c = decode_huffman(code_length_table);
  397. if (c <= 16)
  398. code_sizes[cur++] = static_cast<uint8_t>(c);
  399. else if (c == basisu::cHuffmanSmallZeroRunCode)
  400. cur += get_bits(basisu::cHuffmanSmallZeroRunExtraBits) + basisu::cHuffmanSmallZeroRunSizeMin;
  401. else if (c == basisu::cHuffmanBigZeroRunCode)
  402. cur += get_bits(basisu::cHuffmanBigZeroRunExtraBits) + basisu::cHuffmanBigZeroRunSizeMin;
  403. else
  404. {
  405. if (!cur)
  406. return false;
  407. uint32_t l;
  408. if (c == basisu::cHuffmanSmallRepeatCode)
  409. l = get_bits(basisu::cHuffmanSmallRepeatExtraBits) + basisu::cHuffmanSmallRepeatSizeMin;
  410. else
  411. l = get_bits(basisu::cHuffmanBigRepeatExtraBits) + basisu::cHuffmanBigRepeatSizeMin;
  412. const uint8_t prev = code_sizes[cur - 1];
  413. if (prev == 0)
  414. return false;
  415. do
  416. {
  417. if (cur >= total_used_syms)
  418. return false;
  419. code_sizes[cur++] = prev;
  420. } while (--l > 0);
  421. }
  422. }
  423. if (cur != total_used_syms)
  424. return false;
  425. return ct.init(total_used_syms, &code_sizes[0]);
  426. }
  427. private:
  428. uint32_t m_buf_size;
  429. const uint8_t *m_pBuf;
  430. const uint8_t *m_pBuf_start;
  431. const uint8_t *m_pBuf_end;
  432. uint32_t m_bit_buf;
  433. uint32_t m_bit_buf_size;
  434. };
  435. inline uint32_t basisd_rand(uint32_t seed)
  436. {
  437. if (!seed)
  438. seed++;
  439. uint32_t z = seed;
  440. BASISD_znew;
  441. return z;
  442. }
  443. // Returns random number in [0,limit). Max limit is 0xFFFF.
  444. inline uint32_t basisd_urand(uint32_t& seed, uint32_t limit)
  445. {
  446. seed = basisd_rand(seed);
  447. return (((seed ^ (seed >> 16)) & 0xFFFF) * limit) >> 16;
  448. }
  449. class approx_move_to_front
  450. {
  451. public:
  452. approx_move_to_front(uint32_t n)
  453. {
  454. init(n);
  455. }
  456. void init(uint32_t n)
  457. {
  458. m_values.resize(n);
  459. m_rover = n / 2;
  460. }
  461. const basisu::int_vec& get_values() const { return m_values; }
  462. basisu::int_vec& get_values() { return m_values; }
  463. uint32_t size() const { return (uint32_t)m_values.size(); }
  464. const int& operator[] (uint32_t index) const { return m_values[index]; }
  465. int operator[] (uint32_t index) { return m_values[index]; }
  466. void add(int new_value)
  467. {
  468. m_values[m_rover++] = new_value;
  469. if (m_rover == m_values.size())
  470. m_rover = (uint32_t)m_values.size() / 2;
  471. }
  472. void use(uint32_t index)
  473. {
  474. if (index)
  475. {
  476. //std::swap(m_values[index / 2], m_values[index]);
  477. int x = m_values[index / 2];
  478. int y = m_values[index];
  479. m_values[index / 2] = y;
  480. m_values[index] = x;
  481. }
  482. }
  483. // returns -1 if not found
  484. int find(int value) const
  485. {
  486. for (uint32_t i = 0; i < m_values.size(); i++)
  487. if (m_values[i] == value)
  488. return i;
  489. return -1;
  490. }
  491. void reset()
  492. {
  493. const uint32_t n = (uint32_t)m_values.size();
  494. m_values.clear();
  495. init(n);
  496. }
  497. private:
  498. basisu::int_vec m_values;
  499. uint32_t m_rover;
  500. };
  501. struct decoder_etc_block;
  502. struct color32
  503. {
  504. union
  505. {
  506. struct
  507. {
  508. uint8_t r;
  509. uint8_t g;
  510. uint8_t b;
  511. uint8_t a;
  512. };
  513. uint8_t c[4];
  514. uint32_t m;
  515. };
  516. color32() { }
  517. color32(uint32_t vr, uint32_t vg, uint32_t vb, uint32_t va) { set(vr, vg, vb, va); }
  518. void set(uint32_t vr, uint32_t vg, uint32_t vb, uint32_t va) { c[0] = static_cast<uint8_t>(vr); c[1] = static_cast<uint8_t>(vg); c[2] = static_cast<uint8_t>(vb); c[3] = static_cast<uint8_t>(va); }
  519. uint8_t operator[] (uint32_t idx) const { assert(idx < 4); return c[idx]; }
  520. uint8_t &operator[] (uint32_t idx) { assert(idx < 4); return c[idx]; }
  521. bool operator== (const color32&rhs) const { return m == rhs.m; }
  522. };
  523. struct endpoint
  524. {
  525. color32 m_color5;
  526. uint8_t m_inten5;
  527. };
  528. struct selector
  529. {
  530. // Plain selectors (2-bits per value)
  531. uint8_t m_selectors[4];
  532. // ETC1 selectors
  533. uint8_t m_bytes[4];
  534. uint8_t m_lo_selector, m_hi_selector;
  535. uint8_t m_num_unique_selectors;
  536. void init_flags()
  537. {
  538. uint32_t hist[4] = { 0, 0, 0, 0 };
  539. for (uint32_t y = 0; y < 4; y++)
  540. {
  541. for (uint32_t x = 0; x < 4; x++)
  542. {
  543. uint32_t s = get_selector(x, y);
  544. hist[s]++;
  545. }
  546. }
  547. m_lo_selector = 3;
  548. m_hi_selector = 0;
  549. m_num_unique_selectors = 0;
  550. for (uint32_t i = 0; i < 4; i++)
  551. {
  552. if (hist[i])
  553. {
  554. m_num_unique_selectors++;
  555. if (i < m_lo_selector) m_lo_selector = static_cast<uint8_t>(i);
  556. if (i > m_hi_selector) m_hi_selector = static_cast<uint8_t>(i);
  557. }
  558. }
  559. }
  560. // Returned selector value ranges from 0-3 and is a direct index into g_etc1_inten_tables.
  561. inline uint32_t get_selector(uint32_t x, uint32_t y) const
  562. {
  563. assert((x < 4) && (y < 4));
  564. return (m_selectors[y] >> (x * 2)) & 3;
  565. }
  566. void set_selector(uint32_t x, uint32_t y, uint32_t val)
  567. {
  568. static const uint8_t s_selector_index_to_etc1[4] = { 3, 2, 0, 1 };
  569. assert((x | y | val) < 4);
  570. m_selectors[y] &= ~(3 << (x * 2));
  571. m_selectors[y] |= (val << (x * 2));
  572. const uint32_t etc1_bit_index = x * 4 + y;
  573. uint8_t *p = &m_bytes[3 - (etc1_bit_index >> 3)];
  574. const uint32_t byte_bit_ofs = etc1_bit_index & 7;
  575. const uint32_t mask = 1 << byte_bit_ofs;
  576. const uint32_t etc1_val = s_selector_index_to_etc1[val];
  577. const uint32_t lsb = etc1_val & 1;
  578. const uint32_t msb = etc1_val >> 1;
  579. p[0] &= ~mask;
  580. p[0] |= (lsb << byte_bit_ofs);
  581. p[-2] &= ~mask;
  582. p[-2] |= (msb << byte_bit_ofs);
  583. }
  584. };
  585. } // namespace basist