astcenc_integer_sequence.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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 encoding/decoding Bounded Integer Sequence Encoding.
  19. */
  20. #include "astcenc_internal.h"
  21. #include <array>
  22. /** @brief Unpacked quint triplets <low,middle,high> for each packed value */
  23. // TODO: Bitpack these into a uint16_t?
  24. static const uint8_t quints_of_integer[128][3] {
  25. {0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {3, 0, 0},
  26. {4, 0, 0}, {0, 4, 0}, {4, 4, 0}, {4, 4, 4},
  27. {0, 1, 0}, {1, 1, 0}, {2, 1, 0}, {3, 1, 0},
  28. {4, 1, 0}, {1, 4, 0}, {4, 4, 1}, {4, 4, 4},
  29. {0, 2, 0}, {1, 2, 0}, {2, 2, 0}, {3, 2, 0},
  30. {4, 2, 0}, {2, 4, 0}, {4, 4, 2}, {4, 4, 4},
  31. {0, 3, 0}, {1, 3, 0}, {2, 3, 0}, {3, 3, 0},
  32. {4, 3, 0}, {3, 4, 0}, {4, 4, 3}, {4, 4, 4},
  33. {0, 0, 1}, {1, 0, 1}, {2, 0, 1}, {3, 0, 1},
  34. {4, 0, 1}, {0, 4, 1}, {4, 0, 4}, {0, 4, 4},
  35. {0, 1, 1}, {1, 1, 1}, {2, 1, 1}, {3, 1, 1},
  36. {4, 1, 1}, {1, 4, 1}, {4, 1, 4}, {1, 4, 4},
  37. {0, 2, 1}, {1, 2, 1}, {2, 2, 1}, {3, 2, 1},
  38. {4, 2, 1}, {2, 4, 1}, {4, 2, 4}, {2, 4, 4},
  39. {0, 3, 1}, {1, 3, 1}, {2, 3, 1}, {3, 3, 1},
  40. {4, 3, 1}, {3, 4, 1}, {4, 3, 4}, {3, 4, 4},
  41. {0, 0, 2}, {1, 0, 2}, {2, 0, 2}, {3, 0, 2},
  42. {4, 0, 2}, {0, 4, 2}, {2, 0, 4}, {3, 0, 4},
  43. {0, 1, 2}, {1, 1, 2}, {2, 1, 2}, {3, 1, 2},
  44. {4, 1, 2}, {1, 4, 2}, {2, 1, 4}, {3, 1, 4},
  45. {0, 2, 2}, {1, 2, 2}, {2, 2, 2}, {3, 2, 2},
  46. {4, 2, 2}, {2, 4, 2}, {2, 2, 4}, {3, 2, 4},
  47. {0, 3, 2}, {1, 3, 2}, {2, 3, 2}, {3, 3, 2},
  48. {4, 3, 2}, {3, 4, 2}, {2, 3, 4}, {3, 3, 4},
  49. {0, 0, 3}, {1, 0, 3}, {2, 0, 3}, {3, 0, 3},
  50. {4, 0, 3}, {0, 4, 3}, {0, 0, 4}, {1, 0, 4},
  51. {0, 1, 3}, {1, 1, 3}, {2, 1, 3}, {3, 1, 3},
  52. {4, 1, 3}, {1, 4, 3}, {0, 1, 4}, {1, 1, 4},
  53. {0, 2, 3}, {1, 2, 3}, {2, 2, 3}, {3, 2, 3},
  54. {4, 2, 3}, {2, 4, 3}, {0, 2, 4}, {1, 2, 4},
  55. {0, 3, 3}, {1, 3, 3}, {2, 3, 3}, {3, 3, 3},
  56. {4, 3, 3}, {3, 4, 3}, {0, 3, 4}, {1, 3, 4}
  57. };
  58. /** @brief Packed quint values for each unpacked value, indexed [hi][mid][lo]. */
  59. static const uint8_t integer_of_quints[5][5][5] {
  60. {
  61. {0, 1, 2, 3, 4},
  62. {8, 9, 10, 11, 12},
  63. {16, 17, 18, 19, 20},
  64. {24, 25, 26, 27, 28},
  65. {5, 13, 21, 29, 6}
  66. },
  67. {
  68. {32, 33, 34, 35, 36},
  69. {40, 41, 42, 43, 44},
  70. {48, 49, 50, 51, 52},
  71. {56, 57, 58, 59, 60},
  72. {37, 45, 53, 61, 14}
  73. },
  74. {
  75. {64, 65, 66, 67, 68},
  76. {72, 73, 74, 75, 76},
  77. {80, 81, 82, 83, 84},
  78. {88, 89, 90, 91, 92},
  79. {69, 77, 85, 93, 22}
  80. },
  81. {
  82. {96, 97, 98, 99, 100},
  83. {104, 105, 106, 107, 108},
  84. {112, 113, 114, 115, 116},
  85. {120, 121, 122, 123, 124},
  86. {101, 109, 117, 125, 30}
  87. },
  88. {
  89. {102, 103, 70, 71, 38},
  90. {110, 111, 78, 79, 46},
  91. {118, 119, 86, 87, 54},
  92. {126, 127, 94, 95, 62},
  93. {39, 47, 55, 63, 31}
  94. }
  95. };
  96. /** @brief Unpacked trit quintuplets <low,...,high> for each packed value */
  97. // TODO: Bitpack these into a uint16_t?
  98. static const uint8_t trits_of_integer[256][5] {
  99. {0, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, {2, 0, 0, 0, 0}, {0, 0, 2, 0, 0},
  100. {0, 1, 0, 0, 0}, {1, 1, 0, 0, 0}, {2, 1, 0, 0, 0}, {1, 0, 2, 0, 0},
  101. {0, 2, 0, 0, 0}, {1, 2, 0, 0, 0}, {2, 2, 0, 0, 0}, {2, 0, 2, 0, 0},
  102. {0, 2, 2, 0, 0}, {1, 2, 2, 0, 0}, {2, 2, 2, 0, 0}, {2, 0, 2, 0, 0},
  103. {0, 0, 1, 0, 0}, {1, 0, 1, 0, 0}, {2, 0, 1, 0, 0}, {0, 1, 2, 0, 0},
  104. {0, 1, 1, 0, 0}, {1, 1, 1, 0, 0}, {2, 1, 1, 0, 0}, {1, 1, 2, 0, 0},
  105. {0, 2, 1, 0, 0}, {1, 2, 1, 0, 0}, {2, 2, 1, 0, 0}, {2, 1, 2, 0, 0},
  106. {0, 0, 0, 2, 2}, {1, 0, 0, 2, 2}, {2, 0, 0, 2, 2}, {0, 0, 2, 2, 2},
  107. {0, 0, 0, 1, 0}, {1, 0, 0, 1, 0}, {2, 0, 0, 1, 0}, {0, 0, 2, 1, 0},
  108. {0, 1, 0, 1, 0}, {1, 1, 0, 1, 0}, {2, 1, 0, 1, 0}, {1, 0, 2, 1, 0},
  109. {0, 2, 0, 1, 0}, {1, 2, 0, 1, 0}, {2, 2, 0, 1, 0}, {2, 0, 2, 1, 0},
  110. {0, 2, 2, 1, 0}, {1, 2, 2, 1, 0}, {2, 2, 2, 1, 0}, {2, 0, 2, 1, 0},
  111. {0, 0, 1, 1, 0}, {1, 0, 1, 1, 0}, {2, 0, 1, 1, 0}, {0, 1, 2, 1, 0},
  112. {0, 1, 1, 1, 0}, {1, 1, 1, 1, 0}, {2, 1, 1, 1, 0}, {1, 1, 2, 1, 0},
  113. {0, 2, 1, 1, 0}, {1, 2, 1, 1, 0}, {2, 2, 1, 1, 0}, {2, 1, 2, 1, 0},
  114. {0, 1, 0, 2, 2}, {1, 1, 0, 2, 2}, {2, 1, 0, 2, 2}, {1, 0, 2, 2, 2},
  115. {0, 0, 0, 2, 0}, {1, 0, 0, 2, 0}, {2, 0, 0, 2, 0}, {0, 0, 2, 2, 0},
  116. {0, 1, 0, 2, 0}, {1, 1, 0, 2, 0}, {2, 1, 0, 2, 0}, {1, 0, 2, 2, 0},
  117. {0, 2, 0, 2, 0}, {1, 2, 0, 2, 0}, {2, 2, 0, 2, 0}, {2, 0, 2, 2, 0},
  118. {0, 2, 2, 2, 0}, {1, 2, 2, 2, 0}, {2, 2, 2, 2, 0}, {2, 0, 2, 2, 0},
  119. {0, 0, 1, 2, 0}, {1, 0, 1, 2, 0}, {2, 0, 1, 2, 0}, {0, 1, 2, 2, 0},
  120. {0, 1, 1, 2, 0}, {1, 1, 1, 2, 0}, {2, 1, 1, 2, 0}, {1, 1, 2, 2, 0},
  121. {0, 2, 1, 2, 0}, {1, 2, 1, 2, 0}, {2, 2, 1, 2, 0}, {2, 1, 2, 2, 0},
  122. {0, 2, 0, 2, 2}, {1, 2, 0, 2, 2}, {2, 2, 0, 2, 2}, {2, 0, 2, 2, 2},
  123. {0, 0, 0, 0, 2}, {1, 0, 0, 0, 2}, {2, 0, 0, 0, 2}, {0, 0, 2, 0, 2},
  124. {0, 1, 0, 0, 2}, {1, 1, 0, 0, 2}, {2, 1, 0, 0, 2}, {1, 0, 2, 0, 2},
  125. {0, 2, 0, 0, 2}, {1, 2, 0, 0, 2}, {2, 2, 0, 0, 2}, {2, 0, 2, 0, 2},
  126. {0, 2, 2, 0, 2}, {1, 2, 2, 0, 2}, {2, 2, 2, 0, 2}, {2, 0, 2, 0, 2},
  127. {0, 0, 1, 0, 2}, {1, 0, 1, 0, 2}, {2, 0, 1, 0, 2}, {0, 1, 2, 0, 2},
  128. {0, 1, 1, 0, 2}, {1, 1, 1, 0, 2}, {2, 1, 1, 0, 2}, {1, 1, 2, 0, 2},
  129. {0, 2, 1, 0, 2}, {1, 2, 1, 0, 2}, {2, 2, 1, 0, 2}, {2, 1, 2, 0, 2},
  130. {0, 2, 2, 2, 2}, {1, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 0, 2, 2, 2},
  131. {0, 0, 0, 0, 1}, {1, 0, 0, 0, 1}, {2, 0, 0, 0, 1}, {0, 0, 2, 0, 1},
  132. {0, 1, 0, 0, 1}, {1, 1, 0, 0, 1}, {2, 1, 0, 0, 1}, {1, 0, 2, 0, 1},
  133. {0, 2, 0, 0, 1}, {1, 2, 0, 0, 1}, {2, 2, 0, 0, 1}, {2, 0, 2, 0, 1},
  134. {0, 2, 2, 0, 1}, {1, 2, 2, 0, 1}, {2, 2, 2, 0, 1}, {2, 0, 2, 0, 1},
  135. {0, 0, 1, 0, 1}, {1, 0, 1, 0, 1}, {2, 0, 1, 0, 1}, {0, 1, 2, 0, 1},
  136. {0, 1, 1, 0, 1}, {1, 1, 1, 0, 1}, {2, 1, 1, 0, 1}, {1, 1, 2, 0, 1},
  137. {0, 2, 1, 0, 1}, {1, 2, 1, 0, 1}, {2, 2, 1, 0, 1}, {2, 1, 2, 0, 1},
  138. {0, 0, 1, 2, 2}, {1, 0, 1, 2, 2}, {2, 0, 1, 2, 2}, {0, 1, 2, 2, 2},
  139. {0, 0, 0, 1, 1}, {1, 0, 0, 1, 1}, {2, 0, 0, 1, 1}, {0, 0, 2, 1, 1},
  140. {0, 1, 0, 1, 1}, {1, 1, 0, 1, 1}, {2, 1, 0, 1, 1}, {1, 0, 2, 1, 1},
  141. {0, 2, 0, 1, 1}, {1, 2, 0, 1, 1}, {2, 2, 0, 1, 1}, {2, 0, 2, 1, 1},
  142. {0, 2, 2, 1, 1}, {1, 2, 2, 1, 1}, {2, 2, 2, 1, 1}, {2, 0, 2, 1, 1},
  143. {0, 0, 1, 1, 1}, {1, 0, 1, 1, 1}, {2, 0, 1, 1, 1}, {0, 1, 2, 1, 1},
  144. {0, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {2, 1, 1, 1, 1}, {1, 1, 2, 1, 1},
  145. {0, 2, 1, 1, 1}, {1, 2, 1, 1, 1}, {2, 2, 1, 1, 1}, {2, 1, 2, 1, 1},
  146. {0, 1, 1, 2, 2}, {1, 1, 1, 2, 2}, {2, 1, 1, 2, 2}, {1, 1, 2, 2, 2},
  147. {0, 0, 0, 2, 1}, {1, 0, 0, 2, 1}, {2, 0, 0, 2, 1}, {0, 0, 2, 2, 1},
  148. {0, 1, 0, 2, 1}, {1, 1, 0, 2, 1}, {2, 1, 0, 2, 1}, {1, 0, 2, 2, 1},
  149. {0, 2, 0, 2, 1}, {1, 2, 0, 2, 1}, {2, 2, 0, 2, 1}, {2, 0, 2, 2, 1},
  150. {0, 2, 2, 2, 1}, {1, 2, 2, 2, 1}, {2, 2, 2, 2, 1}, {2, 0, 2, 2, 1},
  151. {0, 0, 1, 2, 1}, {1, 0, 1, 2, 1}, {2, 0, 1, 2, 1}, {0, 1, 2, 2, 1},
  152. {0, 1, 1, 2, 1}, {1, 1, 1, 2, 1}, {2, 1, 1, 2, 1}, {1, 1, 2, 2, 1},
  153. {0, 2, 1, 2, 1}, {1, 2, 1, 2, 1}, {2, 2, 1, 2, 1}, {2, 1, 2, 2, 1},
  154. {0, 2, 1, 2, 2}, {1, 2, 1, 2, 2}, {2, 2, 1, 2, 2}, {2, 1, 2, 2, 2},
  155. {0, 0, 0, 1, 2}, {1, 0, 0, 1, 2}, {2, 0, 0, 1, 2}, {0, 0, 2, 1, 2},
  156. {0, 1, 0, 1, 2}, {1, 1, 0, 1, 2}, {2, 1, 0, 1, 2}, {1, 0, 2, 1, 2},
  157. {0, 2, 0, 1, 2}, {1, 2, 0, 1, 2}, {2, 2, 0, 1, 2}, {2, 0, 2, 1, 2},
  158. {0, 2, 2, 1, 2}, {1, 2, 2, 1, 2}, {2, 2, 2, 1, 2}, {2, 0, 2, 1, 2},
  159. {0, 0, 1, 1, 2}, {1, 0, 1, 1, 2}, {2, 0, 1, 1, 2}, {0, 1, 2, 1, 2},
  160. {0, 1, 1, 1, 2}, {1, 1, 1, 1, 2}, {2, 1, 1, 1, 2}, {1, 1, 2, 1, 2},
  161. {0, 2, 1, 1, 2}, {1, 2, 1, 1, 2}, {2, 2, 1, 1, 2}, {2, 1, 2, 1, 2},
  162. {0, 2, 2, 2, 2}, {1, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 1, 2, 2, 2}
  163. };
  164. /** @brief Packed trit values for each unpacked value, indexed [hi][][][][lo]. */
  165. static const uint8_t integer_of_trits[3][3][3][3][3] {
  166. {
  167. {
  168. {
  169. {0, 1, 2},
  170. {4, 5, 6},
  171. {8, 9, 10}
  172. },
  173. {
  174. {16, 17, 18},
  175. {20, 21, 22},
  176. {24, 25, 26}
  177. },
  178. {
  179. {3, 7, 15},
  180. {19, 23, 27},
  181. {12, 13, 14}
  182. }
  183. },
  184. {
  185. {
  186. {32, 33, 34},
  187. {36, 37, 38},
  188. {40, 41, 42}
  189. },
  190. {
  191. {48, 49, 50},
  192. {52, 53, 54},
  193. {56, 57, 58}
  194. },
  195. {
  196. {35, 39, 47},
  197. {51, 55, 59},
  198. {44, 45, 46}
  199. }
  200. },
  201. {
  202. {
  203. {64, 65, 66},
  204. {68, 69, 70},
  205. {72, 73, 74}
  206. },
  207. {
  208. {80, 81, 82},
  209. {84, 85, 86},
  210. {88, 89, 90}
  211. },
  212. {
  213. {67, 71, 79},
  214. {83, 87, 91},
  215. {76, 77, 78}
  216. }
  217. }
  218. },
  219. {
  220. {
  221. {
  222. {128, 129, 130},
  223. {132, 133, 134},
  224. {136, 137, 138}
  225. },
  226. {
  227. {144, 145, 146},
  228. {148, 149, 150},
  229. {152, 153, 154}
  230. },
  231. {
  232. {131, 135, 143},
  233. {147, 151, 155},
  234. {140, 141, 142}
  235. }
  236. },
  237. {
  238. {
  239. {160, 161, 162},
  240. {164, 165, 166},
  241. {168, 169, 170}
  242. },
  243. {
  244. {176, 177, 178},
  245. {180, 181, 182},
  246. {184, 185, 186}
  247. },
  248. {
  249. {163, 167, 175},
  250. {179, 183, 187},
  251. {172, 173, 174}
  252. }
  253. },
  254. {
  255. {
  256. {192, 193, 194},
  257. {196, 197, 198},
  258. {200, 201, 202}
  259. },
  260. {
  261. {208, 209, 210},
  262. {212, 213, 214},
  263. {216, 217, 218}
  264. },
  265. {
  266. {195, 199, 207},
  267. {211, 215, 219},
  268. {204, 205, 206}
  269. }
  270. }
  271. },
  272. {
  273. {
  274. {
  275. {96, 97, 98},
  276. {100, 101, 102},
  277. {104, 105, 106}
  278. },
  279. {
  280. {112, 113, 114},
  281. {116, 117, 118},
  282. {120, 121, 122}
  283. },
  284. {
  285. {99, 103, 111},
  286. {115, 119, 123},
  287. {108, 109, 110}
  288. }
  289. },
  290. {
  291. {
  292. {224, 225, 226},
  293. {228, 229, 230},
  294. {232, 233, 234}
  295. },
  296. {
  297. {240, 241, 242},
  298. {244, 245, 246},
  299. {248, 249, 250}
  300. },
  301. {
  302. {227, 231, 239},
  303. {243, 247, 251},
  304. {236, 237, 238}
  305. }
  306. },
  307. {
  308. {
  309. {28, 29, 30},
  310. {60, 61, 62},
  311. {92, 93, 94}
  312. },
  313. {
  314. {156, 157, 158},
  315. {188, 189, 190},
  316. {220, 221, 222}
  317. },
  318. {
  319. {31, 63, 127},
  320. {159, 191, 255},
  321. {252, 253, 254}
  322. }
  323. }
  324. }
  325. };
  326. /**
  327. * @brief The number of bits, trits, and quints needed for a quant level.
  328. */
  329. struct btq_count
  330. {
  331. /** @brief The number of bits. */
  332. uint8_t bits:6;
  333. /** @brief The number of trits. */
  334. uint8_t trits:1;
  335. /** @brief The number of quints. */
  336. uint8_t quints:1;
  337. };
  338. /**
  339. * @brief The table of bits, trits, and quints needed for a quant encode.
  340. */
  341. static const std::array<btq_count, 21> btq_counts {{
  342. { 1, 0, 0 }, // QUANT_2
  343. { 0, 1, 0 }, // QUANT_3
  344. { 2, 0, 0 }, // QUANT_4
  345. { 0, 0, 1 }, // QUANT_5
  346. { 1, 1, 0 }, // QUANT_6
  347. { 3, 0, 0 }, // QUANT_8
  348. { 1, 0, 1 }, // QUANT_10
  349. { 2, 1, 0 }, // QUANT_12
  350. { 4, 0, 0 }, // QUANT_16
  351. { 2, 0, 1 }, // QUANT_20
  352. { 3, 1, 0 }, // QUANT_24
  353. { 5, 0, 0 }, // QUANT_32
  354. { 3, 0, 1 }, // QUANT_40
  355. { 4, 1, 0 }, // QUANT_48
  356. { 6, 0, 0 }, // QUANT_64
  357. { 4, 0, 1 }, // QUANT_80
  358. { 5, 1, 0 }, // QUANT_96
  359. { 7, 0, 0 }, // QUANT_128
  360. { 5, 0, 1 }, // QUANT_160
  361. { 6, 1, 0 }, // QUANT_192
  362. { 8, 0, 0 } // QUANT_256
  363. }};
  364. /**
  365. * @brief The sequence scale, round, and divisors needed to compute sizing.
  366. *
  367. * The length of a quantized sequence in bits is:
  368. * (scale * <sequence_len> + round) / divisor
  369. */
  370. struct ise_size
  371. {
  372. /** @brief The scaling parameter. */
  373. uint8_t scale:6;
  374. /** @brief The divisor parameter. */
  375. uint8_t divisor:2;
  376. };
  377. /**
  378. * @brief The table of scale, round, and divisors needed for quant sizing.
  379. */
  380. static const std::array<ise_size, 21> ise_sizes {{
  381. { 1, 0 }, // QUANT_2
  382. { 8, 2 }, // QUANT_3
  383. { 2, 0 }, // QUANT_4
  384. { 7, 1 }, // QUANT_5
  385. { 13, 2 }, // QUANT_6
  386. { 3, 0 }, // QUANT_8
  387. { 10, 1 }, // QUANT_10
  388. { 18, 2 }, // QUANT_12
  389. { 4, 0 }, // QUANT_16
  390. { 13, 1 }, // QUANT_20
  391. { 23, 2 }, // QUANT_24
  392. { 5, 0 }, // QUANT_32
  393. { 16, 1 }, // QUANT_40
  394. { 28, 2 }, // QUANT_48
  395. { 6, 0 }, // QUANT_64
  396. { 19, 1 }, // QUANT_80
  397. { 33, 2 }, // QUANT_96
  398. { 7, 0 }, // QUANT_128
  399. { 22, 1 }, // QUANT_160
  400. { 38, 2 }, // QUANT_192
  401. { 8, 0 } // QUANT_256
  402. }};
  403. /* See header for documentation. */
  404. unsigned int get_ise_sequence_bitcount(
  405. unsigned int character_count,
  406. quant_method quant_level
  407. ) {
  408. // Cope with out-of bounds values - input might be invalid
  409. if (static_cast<size_t>(quant_level) >= ise_sizes.size())
  410. {
  411. // Arbitrary large number that's more than an ASTC block can hold
  412. return 1024;
  413. }
  414. auto& entry = ise_sizes[quant_level];
  415. unsigned int divisor = (entry.divisor << 1) + 1;
  416. return (entry.scale * character_count + divisor - 1) / divisor;
  417. }
  418. /**
  419. * @brief Write up to 8 bits at an arbitrary bit offset.
  420. *
  421. * The stored value is at most 8 bits, but can be stored at an offset of between 0 and 7 bits so may
  422. * span two separate bytes in memory.
  423. *
  424. * @param value The value to write.
  425. * @param bitcount The number of bits to write, starting from LSB.
  426. * @param bitoffset The bit offset to store at, between 0 and 7.
  427. * @param[in,out] ptr The data pointer to write to.
  428. */
  429. static inline void write_bits(
  430. unsigned int value,
  431. unsigned int bitcount,
  432. unsigned int bitoffset,
  433. uint8_t ptr[2]
  434. ) {
  435. unsigned int mask = (1 << bitcount) - 1;
  436. value &= mask;
  437. ptr += bitoffset >> 3;
  438. bitoffset &= 7;
  439. value <<= bitoffset;
  440. mask <<= bitoffset;
  441. mask = ~mask;
  442. ptr[0] &= mask;
  443. ptr[0] |= value;
  444. ptr[1] &= mask >> 8;
  445. ptr[1] |= value >> 8;
  446. }
  447. /**
  448. * @brief Read up to 8 bits at an arbitrary bit offset.
  449. *
  450. * The stored value is at most 8 bits, but can be stored at an offset of between 0 and 7 bits so may
  451. * span two separate bytes in memory.
  452. *
  453. * @param bitcount The number of bits to read.
  454. * @param bitoffset The bit offset to read from, between 0 and 7.
  455. * @param[in,out] ptr The data pointer to read from.
  456. *
  457. * @return The read value.
  458. */
  459. static inline unsigned int read_bits(
  460. unsigned int bitcount,
  461. unsigned int bitoffset,
  462. const uint8_t* ptr
  463. ) {
  464. unsigned int mask = (1 << bitcount) - 1;
  465. ptr += bitoffset >> 3;
  466. bitoffset &= 7;
  467. unsigned int value = ptr[0] | (ptr[1] << 8);
  468. value >>= bitoffset;
  469. value &= mask;
  470. return value;
  471. }
  472. /* See header for documentation. */
  473. void encode_ise(
  474. quant_method quant_level,
  475. unsigned int character_count,
  476. const uint8_t* input_data,
  477. uint8_t* output_data,
  478. unsigned int bit_offset
  479. ) {
  480. promise(character_count > 0);
  481. unsigned int bits = btq_counts[quant_level].bits;
  482. unsigned int trits = btq_counts[quant_level].trits;
  483. unsigned int quints = btq_counts[quant_level].quints;
  484. unsigned int mask = (1 << bits) - 1;
  485. // Write out trits and bits
  486. if (trits)
  487. {
  488. unsigned int i = 0;
  489. unsigned int full_trit_blocks = character_count / 5;
  490. for (unsigned int j = 0; j < full_trit_blocks; j++)
  491. {
  492. unsigned int i4 = input_data[i + 4] >> bits;
  493. unsigned int i3 = input_data[i + 3] >> bits;
  494. unsigned int i2 = input_data[i + 2] >> bits;
  495. unsigned int i1 = input_data[i + 1] >> bits;
  496. unsigned int i0 = input_data[i + 0] >> bits;
  497. uint8_t T = integer_of_trits[i4][i3][i2][i1][i0];
  498. // The max size of a trit bit count is 6, so we can always safely
  499. // pack a single MX value with the following 1 or 2 T bits.
  500. uint8_t pack;
  501. // Element 0 + T0 + T1
  502. pack = (input_data[i++] & mask) | (((T >> 0) & 0x3) << bits);
  503. write_bits(pack, bits + 2, bit_offset, output_data);
  504. bit_offset += bits + 2;
  505. // Element 1 + T2 + T3
  506. pack = (input_data[i++] & mask) | (((T >> 2) & 0x3) << bits);
  507. write_bits(pack, bits + 2, bit_offset, output_data);
  508. bit_offset += bits + 2;
  509. // Element 2 + T4
  510. pack = (input_data[i++] & mask) | (((T >> 4) & 0x1) << bits);
  511. write_bits(pack, bits + 1, bit_offset, output_data);
  512. bit_offset += bits + 1;
  513. // Element 3 + T5 + T6
  514. pack = (input_data[i++] & mask) | (((T >> 5) & 0x3) << bits);
  515. write_bits(pack, bits + 2, bit_offset, output_data);
  516. bit_offset += bits + 2;
  517. // Element 4 + T7
  518. pack = (input_data[i++] & mask) | (((T >> 7) & 0x1) << bits);
  519. write_bits(pack, bits + 1, bit_offset, output_data);
  520. bit_offset += bits + 1;
  521. }
  522. // Loop tail for a partial block
  523. if (i != character_count)
  524. {
  525. // i4 cannot be present - we know the block is partial
  526. // i0 must be present - we know the block isn't empty
  527. unsigned int i4 = 0;
  528. unsigned int i3 = i + 3 >= character_count ? 0 : input_data[i + 3] >> bits;
  529. unsigned int i2 = i + 2 >= character_count ? 0 : input_data[i + 2] >> bits;
  530. unsigned int i1 = i + 1 >= character_count ? 0 : input_data[i + 1] >> bits;
  531. unsigned int i0 = input_data[i + 0] >> bits;
  532. uint8_t T = integer_of_trits[i4][i3][i2][i1][i0];
  533. for (unsigned int j = 0; i < character_count; i++, j++)
  534. {
  535. // Truncated table as this iteration is always partital
  536. static const uint8_t tbits[4] { 2, 2, 1, 2 };
  537. static const uint8_t tshift[4] { 0, 2, 4, 5 };
  538. uint8_t pack = (input_data[i] & mask) |
  539. (((T >> tshift[j]) & ((1 << tbits[j]) - 1)) << bits);
  540. write_bits(pack, bits + tbits[j], bit_offset, output_data);
  541. bit_offset += bits + tbits[j];
  542. }
  543. }
  544. }
  545. // Write out quints and bits
  546. else if (quints)
  547. {
  548. unsigned int i = 0;
  549. unsigned int full_quint_blocks = character_count / 3;
  550. for (unsigned int j = 0; j < full_quint_blocks; j++)
  551. {
  552. unsigned int i2 = input_data[i + 2] >> bits;
  553. unsigned int i1 = input_data[i + 1] >> bits;
  554. unsigned int i0 = input_data[i + 0] >> bits;
  555. uint8_t T = integer_of_quints[i2][i1][i0];
  556. // The max size of a quint bit count is 5, so we can always safely
  557. // pack a single M value with the following 2 or 3 T bits.
  558. uint8_t pack;
  559. // Element 0
  560. pack = (input_data[i++] & mask) | (((T >> 0) & 0x7) << bits);
  561. write_bits(pack, bits + 3, bit_offset, output_data);
  562. bit_offset += bits + 3;
  563. // Element 1
  564. pack = (input_data[i++] & mask) | (((T >> 3) & 0x3) << bits);
  565. write_bits(pack, bits + 2, bit_offset, output_data);
  566. bit_offset += bits + 2;
  567. // Element 2
  568. pack = (input_data[i++] & mask) | (((T >> 5) & 0x3) << bits);
  569. write_bits(pack, bits + 2, bit_offset, output_data);
  570. bit_offset += bits + 2;
  571. }
  572. // Loop tail for a partial block
  573. if (i != character_count)
  574. {
  575. // i2 cannot be present - we know the block is partial
  576. // i0 must be present - we know the block isn't empty
  577. unsigned int i2 = 0;
  578. unsigned int i1 = i + 1 >= character_count ? 0 : input_data[i + 1] >> bits;
  579. unsigned int i0 = input_data[i + 0] >> bits;
  580. uint8_t T = integer_of_quints[i2][i1][i0];
  581. for (unsigned int j = 0; i < character_count; i++, j++)
  582. {
  583. // Truncated table as this iteration is always partital
  584. static const uint8_t tbits[2] { 3, 2 };
  585. static const uint8_t tshift[2] { 0, 3 };
  586. uint8_t pack = (input_data[i] & mask) |
  587. (((T >> tshift[j]) & ((1 << tbits[j]) - 1)) << bits);
  588. write_bits(pack, bits + tbits[j], bit_offset, output_data);
  589. bit_offset += bits + tbits[j];
  590. }
  591. }
  592. }
  593. // Write out just bits
  594. else
  595. {
  596. for (unsigned int i = 0; i < character_count; i++)
  597. {
  598. write_bits(input_data[i], bits, bit_offset, output_data);
  599. bit_offset += bits;
  600. }
  601. }
  602. }
  603. /* See header for documentation. */
  604. void decode_ise(
  605. quant_method quant_level,
  606. unsigned int character_count,
  607. const uint8_t* input_data,
  608. uint8_t* output_data,
  609. unsigned int bit_offset
  610. ) {
  611. promise(character_count > 0);
  612. // Note: due to how the trit/quint-block unpacking is done in this function, we may write more
  613. // temporary results than the number of outputs. The maximum actual number of results is 64 bit,
  614. // but we keep 4 additional character_count of padding.
  615. uint8_t results[68];
  616. uint8_t tq_blocks[22] { 0 }; // Trit-blocks or quint-blocks, must be zeroed
  617. unsigned int bits = btq_counts[quant_level].bits;
  618. unsigned int trits = btq_counts[quant_level].trits;
  619. unsigned int quints = btq_counts[quant_level].quints;
  620. unsigned int lcounter = 0;
  621. unsigned int hcounter = 0;
  622. // Collect bits for each element, as well as bits for any trit-blocks and quint-blocks.
  623. for (unsigned int i = 0; i < character_count; i++)
  624. {
  625. results[i] = static_cast<uint8_t>(read_bits(bits, bit_offset, input_data));
  626. bit_offset += bits;
  627. if (trits)
  628. {
  629. static const uint8_t bits_to_read[5] { 2, 2, 1, 2, 1 };
  630. static const uint8_t block_shift[5] { 0, 2, 4, 5, 7 };
  631. static const uint8_t next_lcounter[5] { 1, 2, 3, 4, 0 };
  632. static const uint8_t hcounter_incr[5] { 0, 0, 0, 0, 1 };
  633. unsigned int tdata = read_bits(bits_to_read[lcounter], bit_offset, input_data);
  634. bit_offset += bits_to_read[lcounter];
  635. tq_blocks[hcounter] |= tdata << block_shift[lcounter];
  636. hcounter += hcounter_incr[lcounter];
  637. lcounter = next_lcounter[lcounter];
  638. }
  639. if (quints)
  640. {
  641. static const uint8_t bits_to_read[3] { 3, 2, 2 };
  642. static const uint8_t block_shift[3] { 0, 3, 5 };
  643. static const uint8_t next_lcounter[3] { 1, 2, 0 };
  644. static const uint8_t hcounter_incr[3] { 0, 0, 1 };
  645. unsigned int tdata = read_bits(bits_to_read[lcounter], bit_offset, input_data);
  646. bit_offset += bits_to_read[lcounter];
  647. tq_blocks[hcounter] |= tdata << block_shift[lcounter];
  648. hcounter += hcounter_incr[lcounter];
  649. lcounter = next_lcounter[lcounter];
  650. }
  651. }
  652. // Unpack trit-blocks or quint-blocks as needed
  653. if (trits)
  654. {
  655. unsigned int trit_blocks = (character_count + 4) / 5;
  656. promise(trit_blocks > 0);
  657. for (unsigned int i = 0; i < trit_blocks; i++)
  658. {
  659. const uint8_t *tritptr = trits_of_integer[tq_blocks[i]];
  660. results[5 * i ] |= tritptr[0] << bits;
  661. results[5 * i + 1] |= tritptr[1] << bits;
  662. results[5 * i + 2] |= tritptr[2] << bits;
  663. results[5 * i + 3] |= tritptr[3] << bits;
  664. results[5 * i + 4] |= tritptr[4] << bits;
  665. }
  666. }
  667. if (quints)
  668. {
  669. unsigned int quint_blocks = (character_count + 2) / 3;
  670. promise(quint_blocks > 0);
  671. for (unsigned int i = 0; i < quint_blocks; i++)
  672. {
  673. const uint8_t *quintptr = quints_of_integer[tq_blocks[i]];
  674. results[3 * i ] |= quintptr[0] << bits;
  675. results[3 * i + 1] |= quintptr[1] << bits;
  676. results[3 * i + 2] |= quintptr[2] << bits;
  677. }
  678. }
  679. for (unsigned int i = 0; i < character_count; i++)
  680. {
  681. output_data[i] = results[i];
  682. }
  683. }