astcenc_symbolic_physical.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. // SPDX-License-Identifier: Apache-2.0
  2. // ----------------------------------------------------------------------------
  3. // Copyright 2011-2023 Arm Limited
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  6. // use this file except in compliance with the License. You may obtain a copy
  7. // 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, WITHOUT
  13. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. // License for the specific language governing permissions and limitations
  15. // under the License.
  16. // ----------------------------------------------------------------------------
  17. /**
  18. * @brief Functions for converting between symbolic and physical encodings.
  19. */
  20. #include "astcenc_internal.h"
  21. #include <cassert>
  22. /**
  23. * @brief Reverse bits in a byte.
  24. *
  25. * @param p The value to reverse.
  26. *
  27. * @return The reversed result.
  28. */
  29. static inline int bitrev8(int p)
  30. {
  31. p = ((p & 0x0F) << 4) | ((p >> 4) & 0x0F);
  32. p = ((p & 0x33) << 2) | ((p >> 2) & 0x33);
  33. p = ((p & 0x55) << 1) | ((p >> 1) & 0x55);
  34. return p;
  35. }
  36. /**
  37. * @brief Read up to 8 bits at an arbitrary bit offset.
  38. *
  39. * The stored value is at most 8 bits, but can be stored at an offset of between 0 and 7 bits so may
  40. * span two separate bytes in memory.
  41. *
  42. * @param bitcount The number of bits to read.
  43. * @param bitoffset The bit offset to read from, between 0 and 7.
  44. * @param[in,out] ptr The data pointer to read from.
  45. *
  46. * @return The read value.
  47. */
  48. static inline int read_bits(
  49. int bitcount,
  50. int bitoffset,
  51. const uint8_t* ptr
  52. ) {
  53. int mask = (1 << bitcount) - 1;
  54. ptr += bitoffset >> 3;
  55. bitoffset &= 7;
  56. int value = ptr[0] | (ptr[1] << 8);
  57. value >>= bitoffset;
  58. value &= mask;
  59. return value;
  60. }
  61. #if !defined(ASTCENC_DECOMPRESS_ONLY)
  62. /**
  63. * @brief Write up to 8 bits at an arbitrary bit offset.
  64. *
  65. * The stored value is at most 8 bits, but can be stored at an offset of between 0 and 7 bits so
  66. * may span two separate bytes in memory.
  67. *
  68. * @param value The value to write.
  69. * @param bitcount The number of bits to write, starting from LSB.
  70. * @param bitoffset The bit offset to store at, between 0 and 7.
  71. * @param[in,out] ptr The data pointer to write to.
  72. */
  73. static inline void write_bits(
  74. int value,
  75. int bitcount,
  76. int bitoffset,
  77. uint8_t* ptr
  78. ) {
  79. int mask = (1 << bitcount) - 1;
  80. value &= mask;
  81. ptr += bitoffset >> 3;
  82. bitoffset &= 7;
  83. value <<= bitoffset;
  84. mask <<= bitoffset;
  85. mask = ~mask;
  86. ptr[0] &= mask;
  87. ptr[0] |= value;
  88. ptr[1] &= mask >> 8;
  89. ptr[1] |= value >> 8;
  90. }
  91. /* See header for documentation. */
  92. void symbolic_to_physical(
  93. const block_size_descriptor& bsd,
  94. const symbolic_compressed_block& scb,
  95. uint8_t pcb[16]
  96. ) {
  97. assert(scb.block_type != SYM_BTYPE_ERROR);
  98. // Constant color block using UNORM16 colors
  99. if (scb.block_type == SYM_BTYPE_CONST_U16)
  100. {
  101. // There is currently no attempt to coalesce larger void-extents
  102. static const uint8_t cbytes[8] { 0xFC, 0xFD, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  103. for (unsigned int i = 0; i < 8; i++)
  104. {
  105. pcb[i] = cbytes[i];
  106. }
  107. for (unsigned int i = 0; i < BLOCK_MAX_COMPONENTS; i++)
  108. {
  109. pcb[2 * i + 8] = scb.constant_color[i] & 0xFF;
  110. pcb[2 * i + 9] = (scb.constant_color[i] >> 8) & 0xFF;
  111. }
  112. return;
  113. }
  114. // Constant color block using FP16 colors
  115. if (scb.block_type == SYM_BTYPE_CONST_F16)
  116. {
  117. // There is currently no attempt to coalesce larger void-extents
  118. static const uint8_t cbytes[8] { 0xFC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  119. for (unsigned int i = 0; i < 8; i++)
  120. {
  121. pcb[i] = cbytes[i];
  122. }
  123. for (unsigned int i = 0; i < BLOCK_MAX_COMPONENTS; i++)
  124. {
  125. pcb[2 * i + 8] = scb.constant_color[i] & 0xFF;
  126. pcb[2 * i + 9] = (scb.constant_color[i] >> 8) & 0xFF;
  127. }
  128. return;
  129. }
  130. unsigned int partition_count = scb.partition_count;
  131. // Compress the weights.
  132. // They are encoded as an ordinary integer-sequence, then bit-reversed
  133. uint8_t weightbuf[16] { 0 };
  134. const auto& bm = bsd.get_block_mode(scb.block_mode);
  135. const auto& di = bsd.get_decimation_info(bm.decimation_mode);
  136. int weight_count = di.weight_count;
  137. quant_method weight_quant_method = bm.get_weight_quant_mode();
  138. float weight_quant_levels = static_cast<float>(get_quant_level(weight_quant_method));
  139. int is_dual_plane = bm.is_dual_plane;
  140. const auto& qat = quant_and_xfer_tables[weight_quant_method];
  141. int real_weight_count = is_dual_plane ? 2 * weight_count : weight_count;
  142. int bits_for_weights = get_ise_sequence_bitcount(real_weight_count, weight_quant_method);
  143. uint8_t weights[64];
  144. if (is_dual_plane)
  145. {
  146. for (int i = 0; i < weight_count; i++)
  147. {
  148. float uqw = static_cast<float>(scb.weights[i]);
  149. float qw = (uqw / 64.0f) * (weight_quant_levels - 1.0f);
  150. int qwi = static_cast<int>(qw + 0.5f);
  151. weights[2 * i] = qat.scramble_map[qwi];
  152. uqw = static_cast<float>(scb.weights[i + WEIGHTS_PLANE2_OFFSET]);
  153. qw = (uqw / 64.0f) * (weight_quant_levels - 1.0f);
  154. qwi = static_cast<int>(qw + 0.5f);
  155. weights[2 * i + 1] = qat.scramble_map[qwi];
  156. }
  157. }
  158. else
  159. {
  160. for (int i = 0; i < weight_count; i++)
  161. {
  162. float uqw = static_cast<float>(scb.weights[i]);
  163. float qw = (uqw / 64.0f) * (weight_quant_levels - 1.0f);
  164. int qwi = static_cast<int>(qw + 0.5f);
  165. weights[i] = qat.scramble_map[qwi];
  166. }
  167. }
  168. encode_ise(weight_quant_method, real_weight_count, weights, weightbuf, 0);
  169. for (int i = 0; i < 16; i++)
  170. {
  171. pcb[i] = static_cast<uint8_t>(bitrev8(weightbuf[15 - i]));
  172. }
  173. write_bits(scb.block_mode, 11, 0, pcb);
  174. write_bits(partition_count - 1, 2, 11, pcb);
  175. int below_weights_pos = 128 - bits_for_weights;
  176. // Encode partition index and color endpoint types for blocks with 2+ partitions
  177. if (partition_count > 1)
  178. {
  179. write_bits(scb.partition_index, 6, 13, pcb);
  180. write_bits(scb.partition_index >> 6, PARTITION_INDEX_BITS - 6, 19, pcb);
  181. if (scb.color_formats_matched)
  182. {
  183. write_bits(scb.color_formats[0] << 2, 6, 13 + PARTITION_INDEX_BITS, pcb);
  184. }
  185. else
  186. {
  187. // Check endpoint types for each partition to determine the lowest class present
  188. int low_class = 4;
  189. for (unsigned int i = 0; i < partition_count; i++)
  190. {
  191. int class_of_format = scb.color_formats[i] >> 2;
  192. low_class = astc::min(class_of_format, low_class);
  193. }
  194. if (low_class == 3)
  195. {
  196. low_class = 2;
  197. }
  198. int encoded_type = low_class + 1;
  199. int bitpos = 2;
  200. for (unsigned int i = 0; i < partition_count; i++)
  201. {
  202. int classbit_of_format = (scb.color_formats[i] >> 2) - low_class;
  203. encoded_type |= classbit_of_format << bitpos;
  204. bitpos++;
  205. }
  206. for (unsigned int i = 0; i < partition_count; i++)
  207. {
  208. int lowbits_of_format = scb.color_formats[i] & 3;
  209. encoded_type |= lowbits_of_format << bitpos;
  210. bitpos += 2;
  211. }
  212. int encoded_type_lowpart = encoded_type & 0x3F;
  213. int encoded_type_highpart = encoded_type >> 6;
  214. int encoded_type_highpart_size = (3 * partition_count) - 4;
  215. int encoded_type_highpart_pos = 128 - bits_for_weights - encoded_type_highpart_size;
  216. write_bits(encoded_type_lowpart, 6, 13 + PARTITION_INDEX_BITS, pcb);
  217. write_bits(encoded_type_highpart, encoded_type_highpart_size, encoded_type_highpart_pos, pcb);
  218. below_weights_pos -= encoded_type_highpart_size;
  219. }
  220. }
  221. else
  222. {
  223. write_bits(scb.color_formats[0], 4, 13, pcb);
  224. }
  225. // In dual-plane mode, encode the color component of the second plane of weights
  226. if (is_dual_plane)
  227. {
  228. write_bits(scb.plane2_component, 2, below_weights_pos - 2, pcb);
  229. }
  230. // Encode the color components
  231. uint8_t values_to_encode[32];
  232. int valuecount_to_encode = 0;
  233. const uint8_t* pack_table = color_uquant_to_scrambled_pquant_tables[scb.quant_mode - QUANT_6];
  234. for (unsigned int i = 0; i < scb.partition_count; i++)
  235. {
  236. int vals = 2 * (scb.color_formats[i] >> 2) + 2;
  237. assert(vals <= 8);
  238. for (int j = 0; j < vals; j++)
  239. {
  240. values_to_encode[j + valuecount_to_encode] = pack_table[scb.color_values[i][j]];
  241. }
  242. valuecount_to_encode += vals;
  243. }
  244. encode_ise(scb.get_color_quant_mode(), valuecount_to_encode, values_to_encode, pcb,
  245. scb.partition_count == 1 ? 17 : 19 + PARTITION_INDEX_BITS);
  246. }
  247. #endif
  248. /* See header for documentation. */
  249. void physical_to_symbolic(
  250. const block_size_descriptor& bsd,
  251. const uint8_t pcb[16],
  252. symbolic_compressed_block& scb
  253. ) {
  254. uint8_t bswapped[16];
  255. scb.block_type = SYM_BTYPE_NONCONST;
  256. // Extract header fields
  257. int block_mode = read_bits(11, 0, pcb);
  258. if ((block_mode & 0x1FF) == 0x1FC)
  259. {
  260. // Constant color block
  261. // Check what format the data has
  262. if (block_mode & 0x200)
  263. {
  264. scb.block_type = SYM_BTYPE_CONST_F16;
  265. }
  266. else
  267. {
  268. scb.block_type = SYM_BTYPE_CONST_U16;
  269. }
  270. scb.partition_count = 0;
  271. for (int i = 0; i < 4; i++)
  272. {
  273. scb.constant_color[i] = pcb[2 * i + 8] | (pcb[2 * i + 9] << 8);
  274. }
  275. // Additionally, check that the void-extent
  276. if (bsd.zdim == 1)
  277. {
  278. // 2D void-extent
  279. int rsvbits = read_bits(2, 10, pcb);
  280. if (rsvbits != 3)
  281. {
  282. scb.block_type = SYM_BTYPE_ERROR;
  283. return;
  284. }
  285. // Low values span 3 bytes so need two read_bits calls
  286. int vx_low_s = read_bits(8, 12, pcb) | (read_bits(5, 12 + 8, pcb) << 8);
  287. int vx_high_s = read_bits(13, 25, pcb);
  288. int vx_low_t = read_bits(8, 38, pcb) | (read_bits(5, 38 + 8, pcb) << 8);
  289. int vx_high_t = read_bits(13, 51, pcb);
  290. int all_ones = vx_low_s == 0x1FFF && vx_high_s == 0x1FFF &&
  291. vx_low_t == 0x1FFF && vx_high_t == 0x1FFF;
  292. if ((vx_low_s >= vx_high_s || vx_low_t >= vx_high_t) && !all_ones)
  293. {
  294. scb.block_type = SYM_BTYPE_ERROR;
  295. return;
  296. }
  297. }
  298. else
  299. {
  300. // 3D void-extent
  301. int vx_low_s = read_bits(9, 10, pcb);
  302. int vx_high_s = read_bits(9, 19, pcb);
  303. int vx_low_t = read_bits(9, 28, pcb);
  304. int vx_high_t = read_bits(9, 37, pcb);
  305. int vx_low_r = read_bits(9, 46, pcb);
  306. int vx_high_r = read_bits(9, 55, pcb);
  307. int all_ones = vx_low_s == 0x1FF && vx_high_s == 0x1FF &&
  308. vx_low_t == 0x1FF && vx_high_t == 0x1FF &&
  309. vx_low_r == 0x1FF && vx_high_r == 0x1FF;
  310. if ((vx_low_s >= vx_high_s || vx_low_t >= vx_high_t || vx_low_r >= vx_high_r) && !all_ones)
  311. {
  312. scb.block_type = SYM_BTYPE_ERROR;
  313. return;
  314. }
  315. }
  316. return;
  317. }
  318. unsigned int packed_index = bsd.block_mode_packed_index[block_mode];
  319. if (packed_index == BLOCK_BAD_BLOCK_MODE)
  320. {
  321. scb.block_type = SYM_BTYPE_ERROR;
  322. return;
  323. }
  324. const auto& bm = bsd.get_block_mode(block_mode);
  325. const auto& di = bsd.get_decimation_info(bm.decimation_mode);
  326. int weight_count = di.weight_count;
  327. promise(weight_count > 0);
  328. quant_method weight_quant_method = static_cast<quant_method>(bm.quant_mode);
  329. int is_dual_plane = bm.is_dual_plane;
  330. int real_weight_count = is_dual_plane ? 2 * weight_count : weight_count;
  331. int partition_count = read_bits(2, 11, pcb) + 1;
  332. promise(partition_count > 0);
  333. scb.block_mode = static_cast<uint16_t>(block_mode);
  334. scb.partition_count = static_cast<uint8_t>(partition_count);
  335. for (int i = 0; i < 16; i++)
  336. {
  337. bswapped[i] = static_cast<uint8_t>(bitrev8(pcb[15 - i]));
  338. }
  339. int bits_for_weights = get_ise_sequence_bitcount(real_weight_count, weight_quant_method);
  340. int below_weights_pos = 128 - bits_for_weights;
  341. uint8_t indices[64];
  342. const auto& qat = quant_and_xfer_tables[weight_quant_method];
  343. decode_ise(weight_quant_method, real_weight_count, bswapped, indices, 0);
  344. if (is_dual_plane)
  345. {
  346. for (int i = 0; i < weight_count; i++)
  347. {
  348. scb.weights[i] = qat.unscramble_and_unquant_map[indices[2 * i]];
  349. scb.weights[i + WEIGHTS_PLANE2_OFFSET] = qat.unscramble_and_unquant_map[indices[2 * i + 1]];
  350. }
  351. }
  352. else
  353. {
  354. for (int i = 0; i < weight_count; i++)
  355. {
  356. scb.weights[i] = qat.unscramble_and_unquant_map[indices[i]];
  357. }
  358. }
  359. if (is_dual_plane && partition_count == 4)
  360. {
  361. scb.block_type = SYM_BTYPE_ERROR;
  362. return;
  363. }
  364. scb.color_formats_matched = 0;
  365. // Determine the format of each endpoint pair
  366. int color_formats[BLOCK_MAX_PARTITIONS];
  367. int encoded_type_highpart_size = 0;
  368. if (partition_count == 1)
  369. {
  370. color_formats[0] = read_bits(4, 13, pcb);
  371. scb.partition_index = 0;
  372. }
  373. else
  374. {
  375. encoded_type_highpart_size = (3 * partition_count) - 4;
  376. below_weights_pos -= encoded_type_highpart_size;
  377. int encoded_type = read_bits(6, 13 + PARTITION_INDEX_BITS, pcb) |
  378. (read_bits(encoded_type_highpart_size, below_weights_pos, pcb) << 6);
  379. int baseclass = encoded_type & 0x3;
  380. if (baseclass == 0)
  381. {
  382. for (int i = 0; i < partition_count; i++)
  383. {
  384. color_formats[i] = (encoded_type >> 2) & 0xF;
  385. }
  386. below_weights_pos += encoded_type_highpart_size;
  387. scb.color_formats_matched = 1;
  388. encoded_type_highpart_size = 0;
  389. }
  390. else
  391. {
  392. int bitpos = 2;
  393. baseclass--;
  394. for (int i = 0; i < partition_count; i++)
  395. {
  396. color_formats[i] = (((encoded_type >> bitpos) & 1) + baseclass) << 2;
  397. bitpos++;
  398. }
  399. for (int i = 0; i < partition_count; i++)
  400. {
  401. color_formats[i] |= (encoded_type >> bitpos) & 3;
  402. bitpos += 2;
  403. }
  404. }
  405. scb.partition_index = static_cast<uint16_t>(read_bits(10, 13, pcb));
  406. }
  407. for (int i = 0; i < partition_count; i++)
  408. {
  409. scb.color_formats[i] = static_cast<uint8_t>(color_formats[i]);
  410. }
  411. // Determine number of color endpoint integers
  412. int color_integer_count = 0;
  413. for (int i = 0; i < partition_count; i++)
  414. {
  415. int endpoint_class = color_formats[i] >> 2;
  416. color_integer_count += (endpoint_class + 1) * 2;
  417. }
  418. if (color_integer_count > 18)
  419. {
  420. scb.block_type = SYM_BTYPE_ERROR;
  421. return;
  422. }
  423. // Determine the color endpoint format to use
  424. static const int color_bits_arr[5] { -1, 115 - 4, 113 - 4 - PARTITION_INDEX_BITS, 113 - 4 - PARTITION_INDEX_BITS, 113 - 4 - PARTITION_INDEX_BITS };
  425. int color_bits = color_bits_arr[partition_count] - bits_for_weights - encoded_type_highpart_size;
  426. if (is_dual_plane)
  427. {
  428. color_bits -= 2;
  429. }
  430. if (color_bits < 0)
  431. {
  432. color_bits = 0;
  433. }
  434. int color_quant_level = quant_mode_table[color_integer_count >> 1][color_bits];
  435. if (color_quant_level < QUANT_6)
  436. {
  437. scb.block_type = SYM_BTYPE_ERROR;
  438. return;
  439. }
  440. // Unpack the integer color values and assign to endpoints
  441. scb.quant_mode = static_cast<quant_method>(color_quant_level);
  442. uint8_t values_to_decode[32];
  443. decode_ise(static_cast<quant_method>(color_quant_level), color_integer_count, pcb,
  444. values_to_decode, (partition_count == 1 ? 17 : 19 + PARTITION_INDEX_BITS));
  445. int valuecount_to_decode = 0;
  446. const uint8_t* unpack_table = color_scrambled_pquant_to_uquant_tables[scb.quant_mode - QUANT_6];
  447. for (int i = 0; i < partition_count; i++)
  448. {
  449. int vals = 2 * (color_formats[i] >> 2) + 2;
  450. for (int j = 0; j < vals; j++)
  451. {
  452. scb.color_values[i][j] = unpack_table[values_to_decode[j + valuecount_to_decode]];
  453. }
  454. valuecount_to_decode += vals;
  455. }
  456. // Fetch component for second-plane in the case of dual plane of weights.
  457. scb.plane2_component = -1;
  458. if (is_dual_plane)
  459. {
  460. scb.plane2_component = static_cast<int8_t>(read_bits(2, below_weights_pos - 2, pcb));
  461. }
  462. }