astc_integer_sequence.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /*----------------------------------------------------------------------------*/
  2. /**
  3. * This confidential and proprietary software may be used only as
  4. * authorised by a licensing agreement from ARM Limited
  5. * (C) COPYRIGHT 2011-2012 ARM Limited
  6. * ALL RIGHTS RESERVED
  7. *
  8. * The entire notice above must be reproduced on all authorised
  9. * copies and copies may only be made to the extent permitted
  10. * by a licensing agreement from ARM Limited.
  11. *
  12. * @brief Functions to encode/decode data using Bounded Integer Sequence
  13. * Encoding.
  14. */
  15. /*----------------------------------------------------------------------------*/
  16. #include "astc_codec_internals.h"
  17. // unpacked quint triplets <low,middle,high> for each packed-quint value
  18. static const uint8_t quints_of_integer[128][3] = {
  19. {0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {3, 0, 0},
  20. {4, 0, 0}, {0, 4, 0}, {4, 4, 0}, {4, 4, 4},
  21. {0, 1, 0}, {1, 1, 0}, {2, 1, 0}, {3, 1, 0},
  22. {4, 1, 0}, {1, 4, 0}, {4, 4, 1}, {4, 4, 4},
  23. {0, 2, 0}, {1, 2, 0}, {2, 2, 0}, {3, 2, 0},
  24. {4, 2, 0}, {2, 4, 0}, {4, 4, 2}, {4, 4, 4},
  25. {0, 3, 0}, {1, 3, 0}, {2, 3, 0}, {3, 3, 0},
  26. {4, 3, 0}, {3, 4, 0}, {4, 4, 3}, {4, 4, 4},
  27. {0, 0, 1}, {1, 0, 1}, {2, 0, 1}, {3, 0, 1},
  28. {4, 0, 1}, {0, 4, 1}, {4, 0, 4}, {0, 4, 4},
  29. {0, 1, 1}, {1, 1, 1}, {2, 1, 1}, {3, 1, 1},
  30. {4, 1, 1}, {1, 4, 1}, {4, 1, 4}, {1, 4, 4},
  31. {0, 2, 1}, {1, 2, 1}, {2, 2, 1}, {3, 2, 1},
  32. {4, 2, 1}, {2, 4, 1}, {4, 2, 4}, {2, 4, 4},
  33. {0, 3, 1}, {1, 3, 1}, {2, 3, 1}, {3, 3, 1},
  34. {4, 3, 1}, {3, 4, 1}, {4, 3, 4}, {3, 4, 4},
  35. {0, 0, 2}, {1, 0, 2}, {2, 0, 2}, {3, 0, 2},
  36. {4, 0, 2}, {0, 4, 2}, {2, 0, 4}, {3, 0, 4},
  37. {0, 1, 2}, {1, 1, 2}, {2, 1, 2}, {3, 1, 2},
  38. {4, 1, 2}, {1, 4, 2}, {2, 1, 4}, {3, 1, 4},
  39. {0, 2, 2}, {1, 2, 2}, {2, 2, 2}, {3, 2, 2},
  40. {4, 2, 2}, {2, 4, 2}, {2, 2, 4}, {3, 2, 4},
  41. {0, 3, 2}, {1, 3, 2}, {2, 3, 2}, {3, 3, 2},
  42. {4, 3, 2}, {3, 4, 2}, {2, 3, 4}, {3, 3, 4},
  43. {0, 0, 3}, {1, 0, 3}, {2, 0, 3}, {3, 0, 3},
  44. {4, 0, 3}, {0, 4, 3}, {0, 0, 4}, {1, 0, 4},
  45. {0, 1, 3}, {1, 1, 3}, {2, 1, 3}, {3, 1, 3},
  46. {4, 1, 3}, {1, 4, 3}, {0, 1, 4}, {1, 1, 4},
  47. {0, 2, 3}, {1, 2, 3}, {2, 2, 3}, {3, 2, 3},
  48. {4, 2, 3}, {2, 4, 3}, {0, 2, 4}, {1, 2, 4},
  49. {0, 3, 3}, {1, 3, 3}, {2, 3, 3}, {3, 3, 3},
  50. {4, 3, 3}, {3, 4, 3}, {0, 3, 4}, {1, 3, 4},
  51. };
  52. // packed quint-value for every unpacked quint-triplet
  53. // indexed by [high][middle][low]
  54. static const uint8_t integer_of_quints[5][5][5] = {
  55. {
  56. {0, 1, 2, 3, 4,},
  57. {8, 9, 10, 11, 12,},
  58. {16, 17, 18, 19, 20,},
  59. {24, 25, 26, 27, 28,},
  60. {5, 13, 21, 29, 6,},
  61. },
  62. {
  63. {32, 33, 34, 35, 36,},
  64. {40, 41, 42, 43, 44,},
  65. {48, 49, 50, 51, 52,},
  66. {56, 57, 58, 59, 60,},
  67. {37, 45, 53, 61, 14,},
  68. },
  69. {
  70. {64, 65, 66, 67, 68,},
  71. {72, 73, 74, 75, 76,},
  72. {80, 81, 82, 83, 84,},
  73. {88, 89, 90, 91, 92,},
  74. {69, 77, 85, 93, 22,},
  75. },
  76. {
  77. {96, 97, 98, 99, 100,},
  78. {104, 105, 106, 107, 108,},
  79. {112, 113, 114, 115, 116,},
  80. {120, 121, 122, 123, 124,},
  81. {101, 109, 117, 125, 30,},
  82. },
  83. {
  84. {102, 103, 70, 71, 38,},
  85. {110, 111, 78, 79, 46,},
  86. {118, 119, 86, 87, 54,},
  87. {126, 127, 94, 95, 62,},
  88. {39, 47, 55, 63, 31,},
  89. },
  90. };
  91. // unpacked trit quintuplets <low,_,_,_,high> for each packed-quint value
  92. static const uint8_t trits_of_integer[256][5] = {
  93. {0, 0, 0, 0, 0}, {1, 0, 0, 0, 0}, {2, 0, 0, 0, 0}, {0, 0, 2, 0, 0},
  94. {0, 1, 0, 0, 0}, {1, 1, 0, 0, 0}, {2, 1, 0, 0, 0}, {1, 0, 2, 0, 0},
  95. {0, 2, 0, 0, 0}, {1, 2, 0, 0, 0}, {2, 2, 0, 0, 0}, {2, 0, 2, 0, 0},
  96. {0, 2, 2, 0, 0}, {1, 2, 2, 0, 0}, {2, 2, 2, 0, 0}, {2, 0, 2, 0, 0},
  97. {0, 0, 1, 0, 0}, {1, 0, 1, 0, 0}, {2, 0, 1, 0, 0}, {0, 1, 2, 0, 0},
  98. {0, 1, 1, 0, 0}, {1, 1, 1, 0, 0}, {2, 1, 1, 0, 0}, {1, 1, 2, 0, 0},
  99. {0, 2, 1, 0, 0}, {1, 2, 1, 0, 0}, {2, 2, 1, 0, 0}, {2, 1, 2, 0, 0},
  100. {0, 0, 0, 2, 2}, {1, 0, 0, 2, 2}, {2, 0, 0, 2, 2}, {0, 0, 2, 2, 2},
  101. {0, 0, 0, 1, 0}, {1, 0, 0, 1, 0}, {2, 0, 0, 1, 0}, {0, 0, 2, 1, 0},
  102. {0, 1, 0, 1, 0}, {1, 1, 0, 1, 0}, {2, 1, 0, 1, 0}, {1, 0, 2, 1, 0},
  103. {0, 2, 0, 1, 0}, {1, 2, 0, 1, 0}, {2, 2, 0, 1, 0}, {2, 0, 2, 1, 0},
  104. {0, 2, 2, 1, 0}, {1, 2, 2, 1, 0}, {2, 2, 2, 1, 0}, {2, 0, 2, 1, 0},
  105. {0, 0, 1, 1, 0}, {1, 0, 1, 1, 0}, {2, 0, 1, 1, 0}, {0, 1, 2, 1, 0},
  106. {0, 1, 1, 1, 0}, {1, 1, 1, 1, 0}, {2, 1, 1, 1, 0}, {1, 1, 2, 1, 0},
  107. {0, 2, 1, 1, 0}, {1, 2, 1, 1, 0}, {2, 2, 1, 1, 0}, {2, 1, 2, 1, 0},
  108. {0, 1, 0, 2, 2}, {1, 1, 0, 2, 2}, {2, 1, 0, 2, 2}, {1, 0, 2, 2, 2},
  109. {0, 0, 0, 2, 0}, {1, 0, 0, 2, 0}, {2, 0, 0, 2, 0}, {0, 0, 2, 2, 0},
  110. {0, 1, 0, 2, 0}, {1, 1, 0, 2, 0}, {2, 1, 0, 2, 0}, {1, 0, 2, 2, 0},
  111. {0, 2, 0, 2, 0}, {1, 2, 0, 2, 0}, {2, 2, 0, 2, 0}, {2, 0, 2, 2, 0},
  112. {0, 2, 2, 2, 0}, {1, 2, 2, 2, 0}, {2, 2, 2, 2, 0}, {2, 0, 2, 2, 0},
  113. {0, 0, 1, 2, 0}, {1, 0, 1, 2, 0}, {2, 0, 1, 2, 0}, {0, 1, 2, 2, 0},
  114. {0, 1, 1, 2, 0}, {1, 1, 1, 2, 0}, {2, 1, 1, 2, 0}, {1, 1, 2, 2, 0},
  115. {0, 2, 1, 2, 0}, {1, 2, 1, 2, 0}, {2, 2, 1, 2, 0}, {2, 1, 2, 2, 0},
  116. {0, 2, 0, 2, 2}, {1, 2, 0, 2, 2}, {2, 2, 0, 2, 2}, {2, 0, 2, 2, 2},
  117. {0, 0, 0, 0, 2}, {1, 0, 0, 0, 2}, {2, 0, 0, 0, 2}, {0, 0, 2, 0, 2},
  118. {0, 1, 0, 0, 2}, {1, 1, 0, 0, 2}, {2, 1, 0, 0, 2}, {1, 0, 2, 0, 2},
  119. {0, 2, 0, 0, 2}, {1, 2, 0, 0, 2}, {2, 2, 0, 0, 2}, {2, 0, 2, 0, 2},
  120. {0, 2, 2, 0, 2}, {1, 2, 2, 0, 2}, {2, 2, 2, 0, 2}, {2, 0, 2, 0, 2},
  121. {0, 0, 1, 0, 2}, {1, 0, 1, 0, 2}, {2, 0, 1, 0, 2}, {0, 1, 2, 0, 2},
  122. {0, 1, 1, 0, 2}, {1, 1, 1, 0, 2}, {2, 1, 1, 0, 2}, {1, 1, 2, 0, 2},
  123. {0, 2, 1, 0, 2}, {1, 2, 1, 0, 2}, {2, 2, 1, 0, 2}, {2, 1, 2, 0, 2},
  124. {0, 2, 2, 2, 2}, {1, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 0, 2, 2, 2},
  125. {0, 0, 0, 0, 1}, {1, 0, 0, 0, 1}, {2, 0, 0, 0, 1}, {0, 0, 2, 0, 1},
  126. {0, 1, 0, 0, 1}, {1, 1, 0, 0, 1}, {2, 1, 0, 0, 1}, {1, 0, 2, 0, 1},
  127. {0, 2, 0, 0, 1}, {1, 2, 0, 0, 1}, {2, 2, 0, 0, 1}, {2, 0, 2, 0, 1},
  128. {0, 2, 2, 0, 1}, {1, 2, 2, 0, 1}, {2, 2, 2, 0, 1}, {2, 0, 2, 0, 1},
  129. {0, 0, 1, 0, 1}, {1, 0, 1, 0, 1}, {2, 0, 1, 0, 1}, {0, 1, 2, 0, 1},
  130. {0, 1, 1, 0, 1}, {1, 1, 1, 0, 1}, {2, 1, 1, 0, 1}, {1, 1, 2, 0, 1},
  131. {0, 2, 1, 0, 1}, {1, 2, 1, 0, 1}, {2, 2, 1, 0, 1}, {2, 1, 2, 0, 1},
  132. {0, 0, 1, 2, 2}, {1, 0, 1, 2, 2}, {2, 0, 1, 2, 2}, {0, 1, 2, 2, 2},
  133. {0, 0, 0, 1, 1}, {1, 0, 0, 1, 1}, {2, 0, 0, 1, 1}, {0, 0, 2, 1, 1},
  134. {0, 1, 0, 1, 1}, {1, 1, 0, 1, 1}, {2, 1, 0, 1, 1}, {1, 0, 2, 1, 1},
  135. {0, 2, 0, 1, 1}, {1, 2, 0, 1, 1}, {2, 2, 0, 1, 1}, {2, 0, 2, 1, 1},
  136. {0, 2, 2, 1, 1}, {1, 2, 2, 1, 1}, {2, 2, 2, 1, 1}, {2, 0, 2, 1, 1},
  137. {0, 0, 1, 1, 1}, {1, 0, 1, 1, 1}, {2, 0, 1, 1, 1}, {0, 1, 2, 1, 1},
  138. {0, 1, 1, 1, 1}, {1, 1, 1, 1, 1}, {2, 1, 1, 1, 1}, {1, 1, 2, 1, 1},
  139. {0, 2, 1, 1, 1}, {1, 2, 1, 1, 1}, {2, 2, 1, 1, 1}, {2, 1, 2, 1, 1},
  140. {0, 1, 1, 2, 2}, {1, 1, 1, 2, 2}, {2, 1, 1, 2, 2}, {1, 1, 2, 2, 2},
  141. {0, 0, 0, 2, 1}, {1, 0, 0, 2, 1}, {2, 0, 0, 2, 1}, {0, 0, 2, 2, 1},
  142. {0, 1, 0, 2, 1}, {1, 1, 0, 2, 1}, {2, 1, 0, 2, 1}, {1, 0, 2, 2, 1},
  143. {0, 2, 0, 2, 1}, {1, 2, 0, 2, 1}, {2, 2, 0, 2, 1}, {2, 0, 2, 2, 1},
  144. {0, 2, 2, 2, 1}, {1, 2, 2, 2, 1}, {2, 2, 2, 2, 1}, {2, 0, 2, 2, 1},
  145. {0, 0, 1, 2, 1}, {1, 0, 1, 2, 1}, {2, 0, 1, 2, 1}, {0, 1, 2, 2, 1},
  146. {0, 1, 1, 2, 1}, {1, 1, 1, 2, 1}, {2, 1, 1, 2, 1}, {1, 1, 2, 2, 1},
  147. {0, 2, 1, 2, 1}, {1, 2, 1, 2, 1}, {2, 2, 1, 2, 1}, {2, 1, 2, 2, 1},
  148. {0, 2, 1, 2, 2}, {1, 2, 1, 2, 2}, {2, 2, 1, 2, 2}, {2, 1, 2, 2, 2},
  149. {0, 0, 0, 1, 2}, {1, 0, 0, 1, 2}, {2, 0, 0, 1, 2}, {0, 0, 2, 1, 2},
  150. {0, 1, 0, 1, 2}, {1, 1, 0, 1, 2}, {2, 1, 0, 1, 2}, {1, 0, 2, 1, 2},
  151. {0, 2, 0, 1, 2}, {1, 2, 0, 1, 2}, {2, 2, 0, 1, 2}, {2, 0, 2, 1, 2},
  152. {0, 2, 2, 1, 2}, {1, 2, 2, 1, 2}, {2, 2, 2, 1, 2}, {2, 0, 2, 1, 2},
  153. {0, 0, 1, 1, 2}, {1, 0, 1, 1, 2}, {2, 0, 1, 1, 2}, {0, 1, 2, 1, 2},
  154. {0, 1, 1, 1, 2}, {1, 1, 1, 1, 2}, {2, 1, 1, 1, 2}, {1, 1, 2, 1, 2},
  155. {0, 2, 1, 1, 2}, {1, 2, 1, 1, 2}, {2, 2, 1, 1, 2}, {2, 1, 2, 1, 2},
  156. {0, 2, 2, 2, 2}, {1, 2, 2, 2, 2}, {2, 2, 2, 2, 2}, {2, 1, 2, 2, 2},
  157. };
  158. // packed trit-value for every unpacked trit-quintuplet
  159. // indexed by [high][][][][low]
  160. static const uint8_t integer_of_trits[3][3][3][3][3] = {
  161. {
  162. {
  163. {
  164. {0, 1, 2,},
  165. {4, 5, 6,},
  166. {8, 9, 10,},
  167. },
  168. {
  169. {16, 17, 18,},
  170. {20, 21, 22,},
  171. {24, 25, 26,},
  172. },
  173. {
  174. {3, 7, 15,},
  175. {19, 23, 27,},
  176. {12, 13, 14,},
  177. },
  178. },
  179. {
  180. {
  181. {32, 33, 34,},
  182. {36, 37, 38,},
  183. {40, 41, 42,},
  184. },
  185. {
  186. {48, 49, 50,},
  187. {52, 53, 54,},
  188. {56, 57, 58,},
  189. },
  190. {
  191. {35, 39, 47,},
  192. {51, 55, 59,},
  193. {44, 45, 46,},
  194. },
  195. },
  196. {
  197. {
  198. {64, 65, 66,},
  199. {68, 69, 70,},
  200. {72, 73, 74,},
  201. },
  202. {
  203. {80, 81, 82,},
  204. {84, 85, 86,},
  205. {88, 89, 90,},
  206. },
  207. {
  208. {67, 71, 79,},
  209. {83, 87, 91,},
  210. {76, 77, 78,},
  211. },
  212. },
  213. },
  214. {
  215. {
  216. {
  217. {128, 129, 130,},
  218. {132, 133, 134,},
  219. {136, 137, 138,},
  220. },
  221. {
  222. {144, 145, 146,},
  223. {148, 149, 150,},
  224. {152, 153, 154,},
  225. },
  226. {
  227. {131, 135, 143,},
  228. {147, 151, 155,},
  229. {140, 141, 142,},
  230. },
  231. },
  232. {
  233. {
  234. {160, 161, 162,},
  235. {164, 165, 166,},
  236. {168, 169, 170,},
  237. },
  238. {
  239. {176, 177, 178,},
  240. {180, 181, 182,},
  241. {184, 185, 186,},
  242. },
  243. {
  244. {163, 167, 175,},
  245. {179, 183, 187,},
  246. {172, 173, 174,},
  247. },
  248. },
  249. {
  250. {
  251. {192, 193, 194,},
  252. {196, 197, 198,},
  253. {200, 201, 202,},
  254. },
  255. {
  256. {208, 209, 210,},
  257. {212, 213, 214,},
  258. {216, 217, 218,},
  259. },
  260. {
  261. {195, 199, 207,},
  262. {211, 215, 219,},
  263. {204, 205, 206,},
  264. },
  265. },
  266. },
  267. {
  268. {
  269. {
  270. {96, 97, 98,},
  271. {100, 101, 102,},
  272. {104, 105, 106,},
  273. },
  274. {
  275. {112, 113, 114,},
  276. {116, 117, 118,},
  277. {120, 121, 122,},
  278. },
  279. {
  280. {99, 103, 111,},
  281. {115, 119, 123,},
  282. {108, 109, 110,},
  283. },
  284. },
  285. {
  286. {
  287. {224, 225, 226,},
  288. {228, 229, 230,},
  289. {232, 233, 234,},
  290. },
  291. {
  292. {240, 241, 242,},
  293. {244, 245, 246,},
  294. {248, 249, 250,},
  295. },
  296. {
  297. {227, 231, 239,},
  298. {243, 247, 251,},
  299. {236, 237, 238,},
  300. },
  301. },
  302. {
  303. {
  304. {28, 29, 30,},
  305. {60, 61, 62,},
  306. {92, 93, 94,},
  307. },
  308. {
  309. {156, 157, 158,},
  310. {188, 189, 190,},
  311. {220, 221, 222,},
  312. },
  313. {
  314. {31, 63, 127,},
  315. {159, 191, 255,},
  316. {252, 253, 254,},
  317. },
  318. },
  319. },
  320. };
  321. void find_number_of_bits_trits_quints(int quantization_level, int *bits, int *trits, int *quints)
  322. {
  323. *bits = 0;
  324. *trits = 0;
  325. *quints = 0;
  326. switch (quantization_level)
  327. {
  328. case QUANT_2:
  329. *bits = 1;
  330. break;
  331. case QUANT_3:
  332. *bits = 0;
  333. *trits = 1;
  334. break;
  335. case QUANT_4:
  336. *bits = 2;
  337. break;
  338. case QUANT_5:
  339. *bits = 0;
  340. *quints = 1;
  341. break;
  342. case QUANT_6:
  343. *bits = 1;
  344. *trits = 1;
  345. break;
  346. case QUANT_8:
  347. *bits = 3;
  348. break;
  349. case QUANT_10:
  350. *bits = 1;
  351. *quints = 1;
  352. break;
  353. case QUANT_12:
  354. *bits = 2;
  355. *trits = 1;
  356. break;
  357. case QUANT_16:
  358. *bits = 4;
  359. break;
  360. case QUANT_20:
  361. *bits = 2;
  362. *quints = 1;
  363. break;
  364. case QUANT_24:
  365. *bits = 3;
  366. *trits = 1;
  367. break;
  368. case QUANT_32:
  369. *bits = 5;
  370. break;
  371. case QUANT_40:
  372. *bits = 3;
  373. *quints = 1;
  374. break;
  375. case QUANT_48:
  376. *bits = 4;
  377. *trits = 1;
  378. break;
  379. case QUANT_64:
  380. *bits = 6;
  381. break;
  382. case QUANT_80:
  383. *bits = 4;
  384. *quints = 1;
  385. break;
  386. case QUANT_96:
  387. *bits = 5;
  388. *trits = 1;
  389. break;
  390. case QUANT_128:
  391. *bits = 7;
  392. break;
  393. case QUANT_160:
  394. *bits = 5;
  395. *quints = 1;
  396. break;
  397. case QUANT_192:
  398. *bits = 6;
  399. *trits = 1;
  400. break;
  401. case QUANT_256:
  402. *bits = 8;
  403. break;
  404. }
  405. }
  406. // routine to write up to 8 bits
  407. static inline void write_bits(int value, int bitcount, int bitoffset, uint8_t * ptr)
  408. {
  409. int mask = (1 << bitcount) - 1;
  410. value &= mask;
  411. ptr += bitoffset >> 3;
  412. bitoffset &= 7;
  413. value <<= bitoffset;
  414. mask <<= bitoffset;
  415. mask = ~mask;
  416. ptr[0] &= mask;
  417. ptr[0] |= value;
  418. ptr[1] &= mask >> 8;
  419. ptr[1] |= value >> 8;
  420. }
  421. // routine to read up to 8 bits
  422. static inline int read_bits(int bitcount, int bitoffset, const uint8_t * ptr)
  423. {
  424. int mask = (1 << bitcount) - 1;
  425. ptr += bitoffset >> 3;
  426. bitoffset &= 7;
  427. int value = ptr[0] | (ptr[1] << 8);
  428. value >>= bitoffset;
  429. value &= mask;
  430. return value;
  431. }
  432. void encode_ise(int quantization_level, int elements, const uint8_t * input_data, uint8_t * output_data, int bit_offset)
  433. {
  434. int i;
  435. uint8_t lowparts[64];
  436. uint8_t highparts[69]; // 64 elements + 5 elements for padding
  437. uint8_t tq_blocks[22]; // trit-blocks or quint-blocks
  438. int bits, trits, quints;
  439. find_number_of_bits_trits_quints(quantization_level, &bits, &trits, &quints);
  440. for (i = 0; i < elements; i++)
  441. {
  442. lowparts[i] = input_data[i] & ((1 << bits) - 1);
  443. highparts[i] = input_data[i] >> bits;
  444. }
  445. for (i = elements; i < elements + 5; i++)
  446. highparts[i] = 0; // padding before we start constructing trit-blocks or quint-blocks
  447. // construct trit-blocks or quint-blocks as necessary
  448. if (trits)
  449. {
  450. int trit_blocks = (elements + 4) / 5;
  451. for (i = 0; i < trit_blocks; i++)
  452. tq_blocks[i] = integer_of_trits[highparts[5 * i + 4]][highparts[5 * i + 3]][highparts[5 * i + 2]][highparts[5 * i + 1]][highparts[5 * i]];
  453. }
  454. if (quints)
  455. {
  456. int quint_blocks = (elements + 2) / 3;
  457. for (i = 0; i < quint_blocks; i++)
  458. tq_blocks[i] = integer_of_quints[highparts[3 * i + 2]][highparts[3 * i + 1]][highparts[3 * i]];
  459. }
  460. // then, write out the actual bits.
  461. int lcounter = 0;
  462. int hcounter = 0;
  463. for (i = 0; i < elements; i++)
  464. {
  465. write_bits(lowparts[i], bits, bit_offset, output_data);
  466. bit_offset += bits;
  467. if (trits)
  468. {
  469. static const int bits_to_write[5] = { 2, 2, 1, 2, 1 };
  470. static const int block_shift[5] = { 0, 2, 4, 5, 7 };
  471. static const int next_lcounter[5] = { 1, 2, 3, 4, 0 };
  472. static const int hcounter_incr[5] = { 0, 0, 0, 0, 1 };
  473. write_bits(tq_blocks[hcounter] >> block_shift[lcounter], bits_to_write[lcounter], bit_offset, output_data);
  474. bit_offset += bits_to_write[lcounter];
  475. hcounter += hcounter_incr[lcounter];
  476. lcounter = next_lcounter[lcounter];
  477. }
  478. if (quints)
  479. {
  480. static const int bits_to_write[3] = { 3, 2, 2 };
  481. static const int block_shift[3] = { 0, 3, 5 };
  482. static const int next_lcounter[3] = { 1, 2, 0 };
  483. static const int hcounter_incr[3] = { 0, 0, 1 };
  484. write_bits(tq_blocks[hcounter] >> block_shift[lcounter], bits_to_write[lcounter], bit_offset, output_data);
  485. bit_offset += bits_to_write[lcounter];
  486. hcounter += hcounter_incr[lcounter];
  487. lcounter = next_lcounter[lcounter];
  488. }
  489. }
  490. }
  491. void decode_ise(int quantization_level, int elements, const uint8_t * input_data, uint8_t * output_data, int bit_offset)
  492. {
  493. int i;
  494. // note: due to how the trit/quint-block unpacking is done in this function,
  495. // we may write more temporary results than the number of outputs
  496. // The maximum actual number of results is 64 bit, but we keep 4 additional elements
  497. // of padding.
  498. uint8_t results[68];
  499. uint8_t tq_blocks[22]; // trit-blocks or quint-blocks
  500. int bits, trits, quints;
  501. find_number_of_bits_trits_quints(quantization_level, &bits, &trits, &quints);
  502. int lcounter = 0;
  503. int hcounter = 0;
  504. // trit-blocks or quint-blocks must be zeroed out before we collect them in the loop below.
  505. for (i = 0; i < 22; i++)
  506. tq_blocks[i] = 0;
  507. // collect bits for each element, as well as bits for any trit-blocks and quint-blocks.
  508. for (i = 0; i < elements; i++)
  509. {
  510. results[i] = read_bits(bits, bit_offset, input_data);
  511. bit_offset += bits;
  512. if (trits)
  513. {
  514. static const int bits_to_read[5] = { 2, 2, 1, 2, 1 };
  515. static const int block_shift[5] = { 0, 2, 4, 5, 7 };
  516. static const int next_lcounter[5] = { 1, 2, 3, 4, 0 };
  517. static const int hcounter_incr[5] = { 0, 0, 0, 0, 1 };
  518. int tdata = read_bits(bits_to_read[lcounter], bit_offset, input_data);
  519. bit_offset += bits_to_read[lcounter];
  520. tq_blocks[hcounter] |= tdata << block_shift[lcounter];
  521. hcounter += hcounter_incr[lcounter];
  522. lcounter = next_lcounter[lcounter];
  523. }
  524. if (quints)
  525. {
  526. static const int bits_to_read[3] = { 3, 2, 2 };
  527. static const int block_shift[3] = { 0, 3, 5 };
  528. static const int next_lcounter[3] = { 1, 2, 0 };
  529. static const int hcounter_incr[3] = { 0, 0, 1 };
  530. int tdata = read_bits(bits_to_read[lcounter], bit_offset, input_data);
  531. bit_offset += bits_to_read[lcounter];
  532. tq_blocks[hcounter] |= tdata << block_shift[lcounter];
  533. hcounter += hcounter_incr[lcounter];
  534. lcounter = next_lcounter[lcounter];
  535. }
  536. }
  537. // unpack trit-blocks or quint-blocks as needed
  538. if (trits)
  539. {
  540. int trit_blocks = (elements + 4) / 5;
  541. for (i = 0; i < trit_blocks; i++)
  542. {
  543. const uint8_t *tritptr = trits_of_integer[tq_blocks[i]];
  544. results[5 * i] |= tritptr[0] << bits;
  545. results[5 * i + 1] |= tritptr[1] << bits;
  546. results[5 * i + 2] |= tritptr[2] << bits;
  547. results[5 * i + 3] |= tritptr[3] << bits;
  548. results[5 * i + 4] |= tritptr[4] << bits;
  549. }
  550. }
  551. if (quints)
  552. {
  553. int quint_blocks = (elements + 2) / 3;
  554. for (i = 0; i < quint_blocks; i++)
  555. {
  556. const uint8_t *quintptr = quints_of_integer[tq_blocks[i]];
  557. results[3 * i] |= quintptr[0] << bits;
  558. results[3 * i + 1] |= quintptr[1] << bits;
  559. results[3 * i + 2] |= quintptr[2] << bits;
  560. }
  561. }
  562. for (i = 0; i < elements; i++)
  563. output_data[i] = results[i];
  564. }
  565. int compute_ise_bitcount(int items, quantization_method quant)
  566. {
  567. switch (quant)
  568. {
  569. case QUANT_2:
  570. return items;
  571. case QUANT_3:
  572. return (8 * items + 4) / 5;
  573. case QUANT_4:
  574. return 2 * items;
  575. case QUANT_5:
  576. return (7 * items + 2) / 3;
  577. case QUANT_6:
  578. return (13 * items + 4) / 5;
  579. case QUANT_8:
  580. return 3 * items;
  581. case QUANT_10:
  582. return (10 * items + 2) / 3;
  583. case QUANT_12:
  584. return (18 * items + 4) / 5;
  585. case QUANT_16:
  586. return items * 4;
  587. case QUANT_20:
  588. return (13 * items + 2) / 3;
  589. case QUANT_24:
  590. return (23 * items + 4) / 5;
  591. case QUANT_32:
  592. return 5 * items;
  593. case QUANT_40:
  594. return (16 * items + 2) / 3;
  595. case QUANT_48:
  596. return (28 * items + 4) / 5;
  597. case QUANT_64:
  598. return 6 * items;
  599. case QUANT_80:
  600. return (19 * items + 2) / 3;
  601. case QUANT_96:
  602. return (33 * items + 4) / 5;
  603. case QUANT_128:
  604. return 7 * items;
  605. case QUANT_160:
  606. return (22 * items + 2) / 3;
  607. case QUANT_192:
  608. return (38 * items + 4) / 5;
  609. case QUANT_256:
  610. return 8 * items;
  611. default:
  612. return 100000;
  613. }
  614. }