PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llmessage/patch_code.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 408 lines | 334 code | 37 blank | 37 comment | 36 complexity | bd12f5c223fa0c8df78ae277a0a61e61 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file patch_code.cpp
  3. * @brief Encode patch DCT data into bitcode.
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "llmath.h"
  28. //#include "vmath.h"
  29. #include "v3math.h"
  30. #include "patch_dct.h"
  31. #include "patch_code.h"
  32. #include "bitpack.h"
  33. U32 gPatchSize, gWordBits;
  34. void init_patch_coding(LLBitPack &bitpack)
  35. {
  36. bitpack.resetBitPacking();
  37. }
  38. void code_patch_group_header(LLBitPack &bitpack, LLGroupHeader *gopp)
  39. {
  40. #ifdef LL_BIG_ENDIAN
  41. U8 *stride = (U8 *)&gopp->stride;
  42. bitpack.bitPack(&(stride[1]), 8);
  43. bitpack.bitPack(&(stride[0]), 8);
  44. #else
  45. bitpack.bitPack((U8 *)&gopp->stride, 16);
  46. #endif
  47. bitpack.bitPack((U8 *)&gopp->patch_size, 8);
  48. bitpack.bitPack((U8 *)&gopp->layer_type, 8);
  49. gPatchSize = gopp->patch_size;
  50. }
  51. void code_patch_header(LLBitPack &bitpack, LLPatchHeader *ph, S32 *patch)
  52. {
  53. S32 i, j, temp, patch_size = gPatchSize, wbits = (ph->quant_wbits & 0xf) + 2;
  54. U32 max_wbits = wbits + 5, min_wbits = wbits>>1;
  55. wbits = min_wbits;
  56. for (i = 0; i < (int) patch_size*patch_size; i++)
  57. {
  58. temp = patch[i];
  59. if (temp)
  60. {
  61. if (temp < 0)
  62. temp *= -1;
  63. for (j = max_wbits; j > (int) min_wbits; j--)
  64. {
  65. if (temp & (1<<j))
  66. {
  67. if (j > wbits)
  68. wbits = j;
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. wbits += 1;
  75. ph->quant_wbits &= 0xf0;
  76. if ( (wbits > 17)
  77. ||(wbits < 2))
  78. {
  79. llerrs << "Bits needed per word in code_patch_header out of legal range. Adjust compression quatization." << llendl;
  80. }
  81. ph->quant_wbits |= (wbits - 2);
  82. bitpack.bitPack((U8 *)&ph->quant_wbits, 8);
  83. #ifdef LL_BIG_ENDIAN
  84. U8 *offset = (U8 *)&ph->dc_offset;
  85. bitpack.bitPack(&(offset[3]), 8);
  86. bitpack.bitPack(&(offset[2]), 8);
  87. bitpack.bitPack(&(offset[1]), 8);
  88. bitpack.bitPack(&(offset[0]), 8);
  89. #else
  90. bitpack.bitPack((U8 *)&ph->dc_offset, 32);
  91. #endif
  92. #ifdef LL_BIG_ENDIAN
  93. U8 *range = (U8 *)&ph->range;
  94. bitpack.bitPack(&(range[1]), 8);
  95. bitpack.bitPack(&(range[0]), 8);
  96. #else
  97. bitpack.bitPack((U8 *)&ph->range, 16);
  98. #endif
  99. #ifdef LL_BIG_ENDIAN
  100. U8 *ids = (U8 *)&ph->patchids;
  101. bitpack.bitPack(&(ids[1]), 8);
  102. bitpack.bitPack(&(ids[0]), 2);
  103. #else
  104. bitpack.bitPack((U8 *)&ph->patchids, 10);
  105. #endif
  106. gWordBits = wbits;
  107. }
  108. void code_end_of_data(LLBitPack &bitpack)
  109. {
  110. bitpack.bitPack((U8 *)&END_OF_PATCHES, 8);
  111. }
  112. void code_patch(LLBitPack &bitpack, S32 *patch, S32 postquant)
  113. {
  114. S32 i, j, patch_size = gPatchSize, wbits = gWordBits;
  115. S32 temp;
  116. BOOL b_eob;
  117. if ( (postquant > patch_size*patch_size)
  118. ||(postquant < 0))
  119. {
  120. llerrs << "Bad postquant in code_patch!" << llendl;
  121. }
  122. if (postquant)
  123. patch[patch_size*patch_size - postquant] = 0;
  124. for (i = 0; i < patch_size*patch_size; i++)
  125. {
  126. b_eob = FALSE;
  127. temp = patch[i];
  128. if (!temp)
  129. {
  130. b_eob = TRUE;
  131. for (j = i; j < patch_size*patch_size - postquant; j++)
  132. {
  133. if (patch[j])
  134. {
  135. b_eob = FALSE;
  136. break;
  137. }
  138. }
  139. if (b_eob)
  140. {
  141. bitpack.bitPack((U8 *)&ZERO_EOB, 2);
  142. return;
  143. }
  144. else
  145. {
  146. bitpack.bitPack((U8 *)&ZERO_CODE, 1);
  147. }
  148. }
  149. else
  150. {
  151. if (temp < 0)
  152. {
  153. temp *= -1;
  154. if (temp > (1<<wbits))
  155. {
  156. temp = (1<<wbits);
  157. // printf("patch quatization exceeding allowable bits!");
  158. }
  159. bitpack.bitPack((U8 *)&NEGATIVE_VALUE, 3);
  160. bitpack.bitPack((U8 *)&temp, wbits);
  161. }
  162. else
  163. {
  164. if (temp > (1<<wbits))
  165. {
  166. temp = (1<<wbits);
  167. // printf("patch quatization exceeding allowable bits!");
  168. }
  169. bitpack.bitPack((U8 *)&POSITIVE_VALUE, 3);
  170. bitpack.bitPack((U8 *)&temp, wbits);
  171. }
  172. }
  173. }
  174. }
  175. void end_patch_coding(LLBitPack &bitpack)
  176. {
  177. bitpack.flushBitPack();
  178. }
  179. void init_patch_decoding(LLBitPack &bitpack)
  180. {
  181. bitpack.resetBitPacking();
  182. }
  183. void decode_patch_group_header(LLBitPack &bitpack, LLGroupHeader *gopp)
  184. {
  185. U16 retvalu16;
  186. retvalu16 = 0;
  187. #ifdef LL_BIG_ENDIAN
  188. U8 *ret = (U8 *)&retvalu16;
  189. bitpack.bitUnpack(&(ret[1]), 8);
  190. bitpack.bitUnpack(&(ret[0]), 8);
  191. #else
  192. bitpack.bitUnpack((U8 *)&retvalu16, 16);
  193. #endif
  194. gopp->stride = retvalu16;
  195. U8 retvalu8 = 0;
  196. bitpack.bitUnpack(&retvalu8, 8);
  197. gopp->patch_size = retvalu8;
  198. retvalu8 = 0;
  199. bitpack.bitUnpack(&retvalu8, 8);
  200. gopp->layer_type = retvalu8;
  201. gPatchSize = gopp->patch_size;
  202. }
  203. void decode_patch_header(LLBitPack &bitpack, LLPatchHeader *ph)
  204. {
  205. U8 retvalu8;
  206. retvalu8 = 0;
  207. bitpack.bitUnpack(&retvalu8, 8);
  208. ph->quant_wbits = retvalu8;
  209. if (END_OF_PATCHES == ph->quant_wbits)
  210. {
  211. // End of data, blitz the rest.
  212. ph->dc_offset = 0;
  213. ph->range = 0;
  214. ph->patchids = 0;
  215. return;
  216. }
  217. U32 retvalu32 = 0;
  218. #ifdef LL_BIG_ENDIAN
  219. U8 *ret = (U8 *)&retvalu32;
  220. bitpack.bitUnpack(&(ret[3]), 8);
  221. bitpack.bitUnpack(&(ret[2]), 8);
  222. bitpack.bitUnpack(&(ret[1]), 8);
  223. bitpack.bitUnpack(&(ret[0]), 8);
  224. #else
  225. bitpack.bitUnpack((U8 *)&retvalu32, 32);
  226. #endif
  227. ph->dc_offset = *(F32 *)&retvalu32;
  228. U16 retvalu16 = 0;
  229. #ifdef LL_BIG_ENDIAN
  230. ret = (U8 *)&retvalu16;
  231. bitpack.bitUnpack(&(ret[1]), 8);
  232. bitpack.bitUnpack(&(ret[0]), 8);
  233. #else
  234. bitpack.bitUnpack((U8 *)&retvalu16, 16);
  235. #endif
  236. ph->range = retvalu16;
  237. retvalu16 = 0;
  238. #ifdef LL_BIG_ENDIAN
  239. ret = (U8 *)&retvalu16;
  240. bitpack.bitUnpack(&(ret[1]), 8);
  241. bitpack.bitUnpack(&(ret[0]), 2);
  242. #else
  243. bitpack.bitUnpack((U8 *)&retvalu16, 10);
  244. #endif
  245. ph->patchids = retvalu16;
  246. gWordBits = (ph->quant_wbits & 0xf) + 2;
  247. }
  248. void decode_patch(LLBitPack &bitpack, S32 *patches)
  249. {
  250. #ifdef LL_BIG_ENDIAN
  251. S32 i, j, patch_size = gPatchSize, wbits = gWordBits;
  252. U8 tempu8;
  253. U16 tempu16;
  254. U32 tempu32;
  255. for (i = 0; i < patch_size*patch_size; i++)
  256. {
  257. bitpack.bitUnpack((U8 *)&tempu8, 1);
  258. if (tempu8)
  259. {
  260. // either 0 EOB or Value
  261. bitpack.bitUnpack((U8 *)&tempu8, 1);
  262. if (tempu8)
  263. {
  264. // value
  265. bitpack.bitUnpack((U8 *)&tempu8, 1);
  266. if (tempu8)
  267. {
  268. // negative
  269. patches[i] = -1;
  270. }
  271. else
  272. {
  273. // positive
  274. patches[i] = 1;
  275. }
  276. if (wbits <= 8)
  277. {
  278. bitpack.bitUnpack((U8 *)&tempu8, wbits);
  279. patches[i] *= tempu8;
  280. }
  281. else if (wbits <= 16)
  282. {
  283. tempu16 = 0;
  284. U8 *ret = (U8 *)&tempu16;
  285. bitpack.bitUnpack(&(ret[1]), 8);
  286. bitpack.bitUnpack(&(ret[0]), wbits - 8);
  287. patches[i] *= tempu16;
  288. }
  289. else if (wbits <= 24)
  290. {
  291. tempu32 = 0;
  292. U8 *ret = (U8 *)&tempu32;
  293. bitpack.bitUnpack(&(ret[2]), 8);
  294. bitpack.bitUnpack(&(ret[1]), 8);
  295. bitpack.bitUnpack(&(ret[0]), wbits - 16);
  296. patches[i] *= tempu32;
  297. }
  298. else if (wbits <= 32)
  299. {
  300. tempu32 = 0;
  301. U8 *ret = (U8 *)&tempu32;
  302. bitpack.bitUnpack(&(ret[3]), 8);
  303. bitpack.bitUnpack(&(ret[2]), 8);
  304. bitpack.bitUnpack(&(ret[1]), 8);
  305. bitpack.bitUnpack(&(ret[0]), wbits - 24);
  306. patches[i] *= tempu32;
  307. }
  308. }
  309. else
  310. {
  311. for (j = i; j < patch_size*patch_size; j++)
  312. {
  313. patches[j] = 0;
  314. }
  315. return;
  316. }
  317. }
  318. else
  319. {
  320. patches[i] = 0;
  321. }
  322. }
  323. #else
  324. S32 i, j, patch_size = gPatchSize, wbits = gWordBits;
  325. U32 temp;
  326. for (i = 0; i < patch_size*patch_size; i++)
  327. {
  328. temp = 0;
  329. bitpack.bitUnpack((U8 *)&temp, 1);
  330. if (temp)
  331. {
  332. // either 0 EOB or Value
  333. temp = 0;
  334. bitpack.bitUnpack((U8 *)&temp, 1);
  335. if (temp)
  336. {
  337. // value
  338. temp = 0;
  339. bitpack.bitUnpack((U8 *)&temp, 1);
  340. if (temp)
  341. {
  342. // negative
  343. temp = 0;
  344. bitpack.bitUnpack((U8 *)&temp, wbits);
  345. patches[i] = temp;
  346. patches[i] *= -1;
  347. }
  348. else
  349. {
  350. // positive
  351. temp = 0;
  352. bitpack.bitUnpack((U8 *)&temp, wbits);
  353. patches[i] = temp;
  354. }
  355. }
  356. else
  357. {
  358. for (j = i; j < patch_size*patch_size; j++)
  359. {
  360. patches[j] = 0;
  361. }
  362. return;
  363. }
  364. }
  365. else
  366. {
  367. patches[i] = 0;
  368. }
  369. }
  370. #endif
  371. }