llpatch_code.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /**
  2. * @file llpatch_code.cpp
  3. * @brief Encode patch DCT data into bitcode.
  4. *
  5. * $LicenseInfo:firstyear=2000&license=viewergpl$
  6. *
  7. * Copyright (c) 2000-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llpatch_code.h"
  34. #include "llbitpack.h"
  35. #include "llmath.h"
  36. #include "llvector3.h"
  37. U32 gPatchSize;
  38. U32 gWordBits;
  39. void init_patch_coding(LLBitPack& bitpack)
  40. {
  41. bitpack.resetBitPacking();
  42. }
  43. void code_patch_group_header(LLBitPack& bitpack, LLGroupHeader* gopp)
  44. {
  45. #if LL_BIG_ENDIAN
  46. U8* stride = (U8*)&gopp->stride;
  47. bitpack.bitPack(&(stride[1]), 8);
  48. bitpack.bitPack(&(stride[0]), 8);
  49. #else
  50. bitpack.bitPack((U8*)&gopp->stride, 16);
  51. #endif
  52. bitpack.bitPack((U8*)&gopp->patch_size, 8);
  53. bitpack.bitPack((U8*)&gopp->layer_type, 8);
  54. gPatchSize = gopp->patch_size;
  55. }
  56. void code_patch_header(LLBitPack& bitpack, LLPatchHeader* ph, S32* patch)
  57. {
  58. S32 i, j, temp;
  59. S32 surface = gPatchSize * gPatchSize;
  60. S32 wbits = (ph->quant_wbits & 0xf) + 2;
  61. U32 max_wbits = wbits + 5, min_wbits = wbits>>1;
  62. wbits = min_wbits;
  63. for (i = 0; i < surface; ++i)
  64. {
  65. temp = patch[i];
  66. if (temp)
  67. {
  68. if (temp < 0)
  69. {
  70. temp *= -1;
  71. }
  72. for (j = max_wbits; j > (S32)min_wbits; --j)
  73. {
  74. if (temp & (1 << j))
  75. {
  76. if (j > wbits)
  77. {
  78. wbits = j;
  79. }
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. wbits += 1;
  86. ph->quant_wbits &= 0xf0;
  87. if (wbits > 17 || wbits < 2)
  88. {
  89. llerrs << "Bits needed per word in code_patch_header out of legal range. Adjust compression quantization."
  90. << llendl;
  91. }
  92. ph->quant_wbits |= wbits - 2;
  93. bitpack.bitPack((U8*)&ph->quant_wbits, 8);
  94. #if LL_BIG_ENDIAN
  95. U8* offset = (U8*)&ph->dc_offset;
  96. bitpack.bitPack(&(offset[3]), 8);
  97. bitpack.bitPack(&(offset[2]), 8);
  98. bitpack.bitPack(&(offset[1]), 8);
  99. bitpack.bitPack(&(offset[0]), 8);
  100. #else
  101. bitpack.bitPack((U8*)&ph->dc_offset, 32);
  102. #endif
  103. #if LL_BIG_ENDIAN
  104. U8* range = (U8*)&ph->range;
  105. bitpack.bitPack(&(range[1]), 8);
  106. bitpack.bitPack(&(range[0]), 8);
  107. #else
  108. bitpack.bitPack((U8*)&ph->range, 16);
  109. #endif
  110. #if LL_BIG_ENDIAN
  111. U8* ids = (U8*)&ph->patchids;
  112. bitpack.bitPack(&(ids[1]), 8);
  113. bitpack.bitPack(&(ids[0]), 2);
  114. #else
  115. bitpack.bitPack((U8*)&ph->patchids, 10);
  116. #endif
  117. gWordBits = wbits;
  118. }
  119. void code_end_of_data(LLBitPack& bitpack)
  120. {
  121. bitpack.bitPack((U8*)&END_OF_PATCHES, 8);
  122. }
  123. void code_patch(LLBitPack& bitpack, S32* patch, S32 postquant)
  124. {
  125. S32 i, j;
  126. S32 surface = gPatchSize * gPatchSize;
  127. S32 wbits = gWordBits;
  128. S32 temp;
  129. bool b_eob;
  130. if (postquant > surface || postquant < 0)
  131. {
  132. llerrs << "Bad postquant in code_patch!" << llendl;
  133. }
  134. if (postquant)
  135. {
  136. patch[surface - postquant] = 0;
  137. }
  138. for (i = 0; i < surface; ++i)
  139. {
  140. b_eob = false;
  141. temp = patch[i];
  142. if (!temp)
  143. {
  144. b_eob = true;
  145. for (j = i; j < surface - postquant; ++j)
  146. {
  147. if (patch[j])
  148. {
  149. b_eob = false;
  150. break;
  151. }
  152. }
  153. if (b_eob)
  154. {
  155. bitpack.bitPack((U8*)&ZERO_EOB, 2);
  156. return;
  157. }
  158. else
  159. {
  160. bitpack.bitPack((U8*)&ZERO_CODE, 1);
  161. }
  162. }
  163. else
  164. {
  165. if (temp < 0)
  166. {
  167. temp *= -1;
  168. if (temp > (1 << wbits))
  169. {
  170. temp = 1 << wbits;
  171. }
  172. bitpack.bitPack((U8*)&NEGATIVE_VALUE, 3);
  173. bitpack.bitPack((U8*)&temp, wbits);
  174. }
  175. else
  176. {
  177. if (temp > (1 << wbits))
  178. {
  179. temp = (1 << wbits);
  180. }
  181. bitpack.bitPack((U8*)&POSITIVE_VALUE, 3);
  182. bitpack.bitPack((U8*)&temp, wbits);
  183. }
  184. }
  185. }
  186. }
  187. void end_patch_coding(LLBitPack& bitpack)
  188. {
  189. bitpack.flushBitPack();
  190. }
  191. void init_patch_decoding(LLBitPack& bitpack)
  192. {
  193. bitpack.resetBitPacking();
  194. }
  195. void decode_patch_group_header(LLBitPack& bitpack, LLGroupHeader* gopp)
  196. {
  197. U16 retvalu16 = 0;
  198. #if LL_BIG_ENDIAN
  199. U8 *ret = (U8*)&retvalu16;
  200. bitpack.bitUnpack(&(ret[1]), 8);
  201. bitpack.bitUnpack(&(ret[0]), 8);
  202. #else
  203. bitpack.bitUnpack((U8*)&retvalu16, 16);
  204. #endif
  205. gopp->stride = retvalu16;
  206. U8 retvalu8 = 0;
  207. bitpack.bitUnpack(&retvalu8, 8);
  208. gopp->patch_size = retvalu8;
  209. retvalu8 = 0;
  210. bitpack.bitUnpack(&retvalu8, 8);
  211. gopp->layer_type = retvalu8;
  212. gPatchSize = gopp->patch_size;
  213. }
  214. void decode_patch_header(LLBitPack& bitpack, LLPatchHeader* ph,
  215. bool large_patch)
  216. {
  217. U8 retvalu8 = 0;
  218. bitpack.bitUnpack(&retvalu8, 8);
  219. ph->quant_wbits = retvalu8;
  220. if (END_OF_PATCHES == ph->quant_wbits)
  221. {
  222. // End of data, blitz the rest.
  223. ph->dc_offset = 0;
  224. ph->range = 0;
  225. ph->patchids = 0;
  226. return;
  227. }
  228. U32 retvalu32 = 0;
  229. #if LL_BIG_ENDIAN
  230. U8* ret = (U8*)&retvalu32;
  231. bitpack.bitUnpack(&(ret[3]), 8);
  232. bitpack.bitUnpack(&(ret[2]), 8);
  233. bitpack.bitUnpack(&(ret[1]), 8);
  234. bitpack.bitUnpack(&(ret[0]), 8);
  235. #else
  236. bitpack.bitUnpack((U8*)&retvalu32, 32);
  237. #endif
  238. ph->dc_offset = *(F32*)&retvalu32;
  239. U16 retvalu16 = 0;
  240. #if LL_BIG_ENDIAN
  241. ret = (U8*)&retvalu16;
  242. bitpack.bitUnpack(&(ret[1]), 8);
  243. bitpack.bitUnpack(&(ret[0]), 8);
  244. #else
  245. bitpack.bitUnpack((U8*)&retvalu16, 16);
  246. #endif
  247. ph->range = retvalu16;
  248. retvalu32 = 0;
  249. #if LL_BIG_ENDIAN
  250. ret = (U8*)&retvalu32;
  251. if (large_patch)
  252. {
  253. bitpack.bitUnpack(&(ret[3]), 8);
  254. bitpack.bitUnpack(&(ret[2]), 8);
  255. bitpack.bitUnpack(&(ret[1]), 8);
  256. bitpack.bitUnpack(&(ret[0]), 8);
  257. }
  258. else
  259. {
  260. bitpack.bitUnpack(&(ret[1]), 8);
  261. bitpack.bitUnpack(&(ret[0]), 2);
  262. }
  263. #else
  264. bitpack.bitUnpack((U8*)&retvalu32, large_patch ? 32 : 10);
  265. #endif
  266. ph->patchids = retvalu32;
  267. gWordBits = (ph->quant_wbits & 0xf) + 2;
  268. }
  269. void decode_patch(LLBitPack& bitpack, S32* patches)
  270. {
  271. S32 surface = gPatchSize * gPatchSize;
  272. S32 wbits = gWordBits;
  273. #if LL_BIG_ENDIAN
  274. U8 tempu8;
  275. U16 tempu16;
  276. U32 tempu32;
  277. for (S32 i = 0; i < surface; ++i)
  278. {
  279. bitpack.bitUnpack((U8*)&tempu8, 1);
  280. if (tempu8)
  281. {
  282. // Either 0 EOB or Value
  283. bitpack.bitUnpack((U8*)&tempu8, 1);
  284. if (tempu8)
  285. {
  286. // Value
  287. bitpack.bitUnpack((U8*)&tempu8, 1);
  288. if (tempu8)
  289. {
  290. // Negative
  291. patches[i] = -1;
  292. }
  293. else
  294. {
  295. // Positive
  296. patches[i] = 1;
  297. }
  298. if (wbits <= 8)
  299. {
  300. bitpack.bitUnpack((U8*)&tempu8, wbits);
  301. patches[i] *= tempu8;
  302. }
  303. else if (wbits <= 16)
  304. {
  305. tempu16 = 0;
  306. U8 *ret = (U8*)&tempu16;
  307. bitpack.bitUnpack(&(ret[1]), 8);
  308. bitpack.bitUnpack(&(ret[0]), wbits - 8);
  309. patches[i] *= tempu16;
  310. }
  311. else if (wbits <= 24)
  312. {
  313. tempu32 = 0;
  314. U8 *ret = (U8*)&tempu32;
  315. bitpack.bitUnpack(&(ret[2]), 8);
  316. bitpack.bitUnpack(&(ret[1]), 8);
  317. bitpack.bitUnpack(&(ret[0]), wbits - 16);
  318. patches[i] *= tempu32;
  319. }
  320. else if (wbits <= 32)
  321. {
  322. tempu32 = 0;
  323. U8 *ret = (U8*)&tempu32;
  324. bitpack.bitUnpack(&(ret[3]), 8);
  325. bitpack.bitUnpack(&(ret[2]), 8);
  326. bitpack.bitUnpack(&(ret[1]), 8);
  327. bitpack.bitUnpack(&(ret[0]), wbits - 24);
  328. patches[i] *= tempu32;
  329. }
  330. }
  331. else
  332. {
  333. for (S32 j = i; j < surface; ++j)
  334. {
  335. patches[j] = 0;
  336. }
  337. return;
  338. }
  339. }
  340. else
  341. {
  342. patches[i] = 0;
  343. }
  344. }
  345. #else
  346. U32 temp;
  347. for (S32 i = 0; i < surface; ++i)
  348. {
  349. temp = 0;
  350. bitpack.bitUnpack((U8*)&temp, 1);
  351. if (temp)
  352. {
  353. // Either 0 EOB or Value
  354. temp = 0;
  355. bitpack.bitUnpack((U8*)&temp, 1);
  356. if (temp)
  357. {
  358. // Value
  359. temp = 0;
  360. bitpack.bitUnpack((U8*)&temp, 1);
  361. if (temp)
  362. {
  363. // Negative
  364. temp = 0;
  365. bitpack.bitUnpack((U8*)&temp, wbits);
  366. patches[i] = temp;
  367. patches[i] *= -1;
  368. }
  369. else
  370. {
  371. // Positive
  372. temp = 0;
  373. bitpack.bitUnpack((U8*)&temp, wbits);
  374. patches[i] = temp;
  375. }
  376. }
  377. else
  378. {
  379. for (S32 j = i; j < surface; ++j)
  380. {
  381. patches[j] = 0;
  382. }
  383. return;
  384. }
  385. }
  386. else
  387. {
  388. patches[i] = 0;
  389. }
  390. }
  391. #endif
  392. }