astcenc_integer_sequence.cpp 22 KB

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