astcenc_symbolic_physical.cpp 15 KB

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