bitStream.cc 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "bitStream.h"
  23. #include "collection/vector.h"
  24. #include "math/mPoint.h"
  25. #include "math/mMatrix.h"
  26. #include "math/mQuat.h"
  27. #include "math/mathIO.h"
  28. #include "platform/event.h"
  29. #include "console/consoleObject.h"
  30. static BitStream gPacketStream(NULL, 0);
  31. static U8 gPacketBuffer[MaxPacketDataSize];
  32. // bitstream utility functions
  33. void BitStream::setStringBuffer(char buffer[256])
  34. {
  35. stringBuffer = buffer;
  36. }
  37. BitStream *BitStream::getPacketStream(U32 writeSize)
  38. {
  39. if(!writeSize)
  40. writeSize = MaxPacketDataSize;
  41. gPacketStream.setBuffer(gPacketBuffer, writeSize, MaxPacketDataSize);
  42. gPacketStream.setPosition(0);
  43. return &gPacketStream;
  44. }
  45. void BitStream::sendPacketStream(const NetAddress *addr)
  46. {
  47. Net::sendto(addr, gPacketStream.getBuffer(), gPacketStream.getPosition());
  48. }
  49. // FIXMEFIXMEFIXME MATH
  50. inline bool IsEqual(F32 a, F32 b) { return a == b; }
  51. ResizeBitStream::ResizeBitStream(U32 minSpace, U32 initialSize) : BitStream(NULL, 0, 0)
  52. {
  53. mMinSpace = minSpace;
  54. if(!initialSize)
  55. initialSize = minSpace * 2;
  56. U8 *buf = (U8 *) dMalloc(initialSize);
  57. setBuffer(buf, initialSize, initialSize);
  58. }
  59. ResizeBitStream::~ResizeBitStream()
  60. {
  61. dFree(dataPtr);
  62. }
  63. void ResizeBitStream::validate()
  64. {
  65. if(getPosition() + mMinSpace > (U32)bufSize)
  66. {
  67. bufSize = getPosition() + mMinSpace * 2;
  68. dataPtr = (U8 *) dRealloc(dataPtr, bufSize);
  69. maxReadBitNum = bufSize << 3;
  70. maxWriteBitNum = bufSize << 3;
  71. }
  72. }
  73. class HuffmanProcessor
  74. {
  75. static const U32 csm_charFreqs[256];
  76. bool m_tablesBuilt;
  77. void buildTables();
  78. struct HuffNode {
  79. U32 pop;
  80. S16 index0;
  81. S16 index1;
  82. };
  83. struct HuffLeaf {
  84. U32 pop;
  85. U8 numBits;
  86. U8 symbol;
  87. U32 code; // no code should be longer than 32 bits.
  88. };
  89. // We have to be a bit careful with these, mSince they are pointers...
  90. struct HuffWrap {
  91. HuffNode* pNode;
  92. HuffLeaf* pLeaf;
  93. public:
  94. HuffWrap() : pNode(NULL), pLeaf(NULL) { }
  95. void set(HuffLeaf* in_leaf) { pNode = NULL; pLeaf = in_leaf; }
  96. void set(HuffNode* in_node) { pLeaf = NULL; pNode = in_node; }
  97. U32 getPop() { if (pNode) return pNode->pop; else return pLeaf->pop; }
  98. };
  99. Vector<HuffNode> m_huffNodes;
  100. Vector<HuffLeaf> m_huffLeaves;
  101. S16 determineIndex(HuffWrap&);
  102. void generateCodes(BitStream&, S32, S32);
  103. public:
  104. HuffmanProcessor() : m_tablesBuilt(false) { }
  105. static HuffmanProcessor g_huffProcessor;
  106. bool readHuffBuffer(BitStream* pStream, char* out_pBuffer);
  107. bool writeHuffBuffer(BitStream* pStream, const char* out_pBuffer, S32 maxLen);
  108. };
  109. HuffmanProcessor HuffmanProcessor::g_huffProcessor;
  110. void BitStream::setBuffer(void *bufPtr, S32 size, S32 maxSize)
  111. {
  112. dataPtr = (U8 *) bufPtr;
  113. bitNum = 0;
  114. bufSize = size;
  115. maxReadBitNum = size << 3;
  116. if(maxSize < 0)
  117. maxSize = size;
  118. maxWriteBitNum = maxSize << 3;
  119. error = false;
  120. mCompressRelative = false;
  121. }
  122. U32 BitStream::getPosition() const
  123. {
  124. return (bitNum + 7) >> 3;
  125. }
  126. bool BitStream::setPosition(const U32 pos)
  127. {
  128. bitNum = pos << 3;
  129. return (true);
  130. }
  131. U32 BitStream::getStreamSize()
  132. {
  133. return bufSize;
  134. }
  135. U8 *BitStream::getBytePtr()
  136. {
  137. return dataPtr + getPosition();
  138. }
  139. U32 BitStream::getReadByteSize()
  140. {
  141. return (maxReadBitNum >> 3) - getPosition();
  142. }
  143. void BitStream::clear()
  144. {
  145. dMemset(dataPtr, 0, bufSize);
  146. }
  147. void BitStream::writeClassId(U32 classId, U32 classType, U32 classGroup)
  148. {
  149. AssertFatal(classType < NetClassTypesCount, "Out of range class type.");
  150. AssertFatal(classId < AbstractClassRep::NetClassCount[classGroup][classType], "Out of range class id.");
  151. writeInt(classId, AbstractClassRep::NetClassBitSize[classGroup][classType]);
  152. }
  153. S32 BitStream::readClassId(U32 classType, U32 classGroup)
  154. {
  155. AssertFatal(classType < NetClassTypesCount, "Out of range class type.");
  156. S32 ret = readInt(AbstractClassRep::NetClassBitSize[classGroup][classType]);
  157. if((U32)ret > AbstractClassRep::NetClassCount[classGroup][classType])
  158. return -1;
  159. return ret;
  160. }
  161. void BitStream::writeBits(S32 bitCount, const void *bitPtr)
  162. {
  163. if(!bitCount)
  164. return;
  165. if(bitCount + bitNum > maxWriteBitNum)
  166. {
  167. error = true;
  168. AssertFatal(false, "Out of range write");
  169. return;
  170. }
  171. // [tom, 8/17/2006] This is probably a lot lamer then it needs to be. However,
  172. // at least it doesnt clobber data or overrun the buffer like the old code did.
  173. const U8 *ptr = (U8 *)bitPtr;
  174. for(S32 srcBitNum = 0;srcBitNum < bitCount;srcBitNum++)
  175. {
  176. if((*(ptr + (srcBitNum >> 3)) & (1 << (srcBitNum & 0x7))) != 0)
  177. *(dataPtr + (bitNum >> 3)) |= (1 << (bitNum & 0x7));
  178. else
  179. *(dataPtr + (bitNum >> 3)) &= ~(1 << (bitNum & 0x7));
  180. bitNum++;
  181. }
  182. }
  183. void BitStream::setBit(S32 bitCount, bool set)
  184. {
  185. if(set)
  186. *(dataPtr + (bitCount >> 3)) |= (1 << (bitCount & 0x7));
  187. else
  188. *(dataPtr + (bitCount >> 3)) &= ~(1 << (bitCount & 0x7));
  189. }
  190. bool BitStream::testBit(S32 bitCount)
  191. {
  192. return (*(dataPtr + (bitCount >> 3)) & (1 << (bitCount & 0x7))) != 0;
  193. }
  194. bool BitStream::writeFlag(bool val)
  195. {
  196. if(bitNum + 1 > maxWriteBitNum)
  197. {
  198. error = true;
  199. AssertFatal(false, "Out of range write");
  200. return false;
  201. }
  202. if(val)
  203. *(dataPtr + (bitNum >> 3)) |= (1 << (bitNum & 0x7));
  204. else
  205. *(dataPtr + (bitNum >> 3)) &= ~(1 << (bitNum & 0x7));
  206. bitNum++;
  207. return (val);
  208. }
  209. void BitStream::readBits(S32 bitCount, void *bitPtr)
  210. {
  211. if(!bitCount)
  212. return;
  213. if(bitCount + bitNum > maxReadBitNum)
  214. {
  215. error = true;
  216. //AssertFatal(false, "Out of range read");
  217. AssertWarn(false, "Out of range read");
  218. return;
  219. }
  220. U8 *stPtr = dataPtr + (bitNum >> 3);
  221. S32 byteCount = (bitCount + 7) >> 3;
  222. U8 *ptr = (U8 *) bitPtr;
  223. S32 downShift = bitNum & 0x7;
  224. S32 upShift = 8 - downShift;
  225. U8 curB = *stPtr;
  226. while(byteCount--)
  227. {
  228. U8 nextB = *++stPtr;
  229. *ptr++ = (curB >> downShift) | (nextB << upShift);
  230. curB = nextB;
  231. }
  232. bitNum += bitCount;
  233. }
  234. bool BitStream::_read(U32 size, void *dataPtr)
  235. {
  236. readBits(size << 3, dataPtr);
  237. return true;
  238. }
  239. bool BitStream::_write(U32 size, const void *dataPtr)
  240. {
  241. writeBits(size << 3, dataPtr);
  242. return true;
  243. }
  244. S32 BitStream::readInt(S32 bitCount)
  245. {
  246. S32 ret = 0;
  247. readBits(bitCount, &ret);
  248. ret = convertLEndianToHost(ret);
  249. if(bitCount == 32)
  250. return ret;
  251. else
  252. ret &= (1 << bitCount) - 1;
  253. return ret;
  254. }
  255. void BitStream::writeInt(S32 val, S32 bitCount)
  256. {
  257. val = convertHostToLEndian(val);
  258. writeBits(bitCount, &val);
  259. }
  260. void BitStream::writeFloat(F32 f, S32 bitCount)
  261. {
  262. writeInt((S32)(f * ((1 << bitCount) - 1)), bitCount);
  263. }
  264. F32 BitStream::readFloat(S32 bitCount)
  265. {
  266. return readInt(bitCount) / F32((1 << bitCount) - 1);
  267. }
  268. void BitStream::writeSignedFloat(F32 f, S32 bitCount)
  269. {
  270. writeInt((S32)(((f + 1) * .5) * ((1 << bitCount) - 1)), bitCount);
  271. }
  272. F32 BitStream::readSignedFloat(S32 bitCount)
  273. {
  274. return readInt(bitCount) * 2 / F32((1 << bitCount) - 1) - 1.0f;
  275. }
  276. void BitStream::writeSignedInt(S32 value, S32 bitCount)
  277. {
  278. if(writeFlag(value < 0))
  279. writeInt(-value, bitCount - 1);
  280. else
  281. writeInt(value, bitCount - 1);
  282. }
  283. S32 BitStream::readSignedInt(S32 bitCount)
  284. {
  285. if(readFlag())
  286. return -readInt(bitCount - 1);
  287. else
  288. return readInt(bitCount - 1);
  289. }
  290. void BitStream::writeNormalVector(const Point3F& vec, S32 bitCount)
  291. {
  292. F32 phi = mAtan(vec.x, vec.y) / (F32)M_PI;
  293. F32 theta = mAtan(vec.z, mSqrt(vec.x*vec.x + vec.y*vec.y)) / ((F32)M_PI/2.0f);
  294. writeSignedFloat(phi, bitCount+1);
  295. writeSignedFloat(theta, bitCount);
  296. }
  297. void BitStream::readNormalVector(Point3F *vec, S32 bitCount)
  298. {
  299. F32 phi = readSignedFloat(bitCount+1) * (U32)M_PI;
  300. F32 theta = readSignedFloat(bitCount) * ((F32)M_PI/2.0f);
  301. vec->x = mSin(phi)*mCos(theta);
  302. vec->y = mCos(phi)*mCos(theta);
  303. vec->z = mSin(theta);
  304. }
  305. Point3F BitStream::dumbDownNormal(const Point3F& vec, S32 bitCount)
  306. {
  307. U8 buffer[128];
  308. BitStream temp(buffer, 128);
  309. temp.writeNormalVector(vec, bitCount);
  310. temp.setCurPos(0);
  311. Point3F ret;
  312. temp.readNormalVector(&ret, bitCount);
  313. return ret;
  314. }
  315. void BitStream::writeNormalVector(const Point3F& vec, S32 angleBitCount, S32 zBitCount)
  316. {
  317. writeSignedFloat( vec.z, zBitCount );
  318. // don't need to write x and y if they are both zero, which we can assess
  319. // by checking for |z| == 1
  320. if(!IsEqual(mFabs(vec.z), 1.0f))
  321. {
  322. writeSignedFloat( mAtan(vec.x,vec.y) / (F32)M_2PI, angleBitCount );
  323. }
  324. else
  325. {
  326. // angle won't matter...
  327. writeSignedFloat(0.0f,angleBitCount);
  328. }
  329. }
  330. void BitStream::readNormalVector(Point3F * vec, S32 angleBitCount, S32 zBitCount)
  331. {
  332. vec->z = readSignedFloat(zBitCount);
  333. F32 angle = (F32)M_2PI * readSignedFloat(angleBitCount);
  334. F32 mult = mSqrt(1.0f - vec->z * vec->z);
  335. vec->x = mult * mSin(angle);
  336. vec->y = mult * mCos(angle);
  337. }
  338. void BitStream::writeAffineTransform(const MatrixF& matrix)
  339. {
  340. // AssertFatal(matrix.isAffine() == true,
  341. // "BitStream::writeAffineTransform: Error, must write only affine transforms!");
  342. Point3F pos;
  343. matrix.getColumn(3, &pos);
  344. mathWrite(*this, pos);
  345. QuatF q(matrix);
  346. q.normalize();
  347. write(q.x);
  348. write(q.y);
  349. write(q.z);
  350. writeFlag(q.w < 0.0);
  351. }
  352. void BitStream::readAffineTransform(MatrixF* matrix)
  353. {
  354. Point3F pos;
  355. QuatF q;
  356. mathRead(*this, &pos);
  357. read(&q.x);
  358. read(&q.y);
  359. read(&q.z);
  360. q.w = mSqrt(1.0f - getMin(F32(((q.x * q.x) + (q.y * q.y) + (q.z * q.z))), 1.f));
  361. if (readFlag())
  362. q.w = -q.w;
  363. q.setMatrix(matrix);
  364. matrix->setColumn(3, pos);
  365. // AssertFatal(matrix->isAffine() == true,
  366. // "BitStream::readAffineTransform: Error, transform should be affine after this function!");
  367. }
  368. //----------------------------------------------------------------------------
  369. void BitStream::clearCompressionPoint()
  370. {
  371. mCompressRelative = false;
  372. }
  373. void BitStream::setCompressionPoint(const Point3F& p)
  374. {
  375. mCompressRelative = true;
  376. mCompressPoint = p;
  377. }
  378. static U32 gBitCounts[4] = {
  379. 16, 18, 20, 32
  380. };
  381. void BitStream::writeCompressedPoint(const Point3F& p,F32 scale)
  382. {
  383. // Same # of bits for all axis
  384. Point3F vec;
  385. F32 invScale = 1 / scale;
  386. U32 type;
  387. if(mCompressRelative)
  388. {
  389. vec = p - mCompressPoint;
  390. F32 dist = vec.len() * invScale;
  391. if(dist < (1 << 15))
  392. type = 0;
  393. else if(dist < (1 << 17))
  394. type = 1;
  395. else if(dist < (1 << 19))
  396. type = 2;
  397. else
  398. type = 3;
  399. }
  400. else
  401. type = 3;
  402. writeInt(type, 2);
  403. if (type != 3)
  404. {
  405. type = gBitCounts[type];
  406. writeSignedInt(S32(vec.x * invScale),type);
  407. writeSignedInt(S32(vec.y * invScale),type);
  408. writeSignedInt(S32(vec.z * invScale),type);
  409. }
  410. else
  411. {
  412. write(p.x);
  413. write(p.y);
  414. write(p.z);
  415. }
  416. }
  417. void BitStream::readCompressedPoint(Point3F* p,F32 scale)
  418. {
  419. // Same # of bits for all axis
  420. U32 type = readInt(2);
  421. if(type == 3)
  422. {
  423. read(&p->x);
  424. read(&p->y);
  425. read(&p->z);
  426. }
  427. else
  428. {
  429. type = gBitCounts[type];
  430. p->x = (F32)readSignedInt(type);
  431. p->y = (F32)readSignedInt(type);
  432. p->z = (F32)readSignedInt(type);
  433. p->x = mCompressPoint.x + p->x * scale;
  434. p->y = mCompressPoint.y + p->y * scale;
  435. p->z = mCompressPoint.z + p->z * scale;
  436. }
  437. }
  438. //------------------------------------------------------------------------------
  439. InfiniteBitStream::InfiniteBitStream()
  440. {
  441. //
  442. }
  443. InfiniteBitStream::~InfiniteBitStream()
  444. {
  445. //
  446. }
  447. void InfiniteBitStream::reset()
  448. {
  449. // Rewing back to beginning
  450. setPosition(0);
  451. }
  452. void InfiniteBitStream::validate(U32 upcomingBytes)
  453. {
  454. if(getPosition() + upcomingBytes + mMinSpace > (U32)bufSize)
  455. {
  456. bufSize = getPosition() + upcomingBytes + mMinSpace;
  457. dataPtr = (U8 *) dRealloc(dataPtr, bufSize);
  458. maxReadBitNum = bufSize << 3;
  459. maxWriteBitNum = bufSize << 3;
  460. }
  461. }
  462. void InfiniteBitStream::compact()
  463. {
  464. // Prepare to copy...
  465. U32 oldSize = bufSize;
  466. U8 *tmp = (U8*)dMalloc(bufSize);
  467. // Copy things...
  468. bufSize = getPosition() + mMinSpace * 2;
  469. dMemcpy(tmp, dataPtr, oldSize);
  470. // And clean up.
  471. dFree(dataPtr);
  472. dataPtr = tmp;
  473. maxReadBitNum = bufSize << 3;
  474. maxWriteBitNum = bufSize << 3;
  475. }
  476. void InfiniteBitStream::writeToStream(Stream &s)
  477. {
  478. s.write(getPosition(), dataPtr);
  479. }
  480. //------------------------------------------------------------------------------
  481. void BitStream::readString(char buf[256])
  482. {
  483. if(stringBuffer)
  484. {
  485. if(readFlag())
  486. {
  487. S32 offset = readInt(8);
  488. HuffmanProcessor::g_huffProcessor.readHuffBuffer(this, stringBuffer + offset);
  489. dStrcpy(buf, stringBuffer);
  490. return;
  491. }
  492. }
  493. HuffmanProcessor::g_huffProcessor.readHuffBuffer(this, buf);
  494. if(stringBuffer)
  495. dStrcpy(stringBuffer, buf);
  496. }
  497. void BitStream::writeString(const char *string, S32 maxLen)
  498. {
  499. if(!string)
  500. string = "";
  501. if(stringBuffer)
  502. {
  503. S32 j;
  504. for(j = 0; j < maxLen && stringBuffer[j] == string[j] && string[j];j++)
  505. ;
  506. dStrncpy(stringBuffer, string, maxLen);
  507. stringBuffer[maxLen] = 0;
  508. if(writeFlag(j > 2))
  509. {
  510. writeInt(j, 8);
  511. HuffmanProcessor::g_huffProcessor.writeHuffBuffer(this, string + j, maxLen - j);
  512. return;
  513. }
  514. }
  515. HuffmanProcessor::g_huffProcessor.writeHuffBuffer(this, string, maxLen);
  516. }
  517. void HuffmanProcessor::buildTables()
  518. {
  519. AssertFatal(m_tablesBuilt == false, "Cannot build tables twice!");
  520. m_tablesBuilt = true;
  521. S32 i;
  522. // First, construct the array of wraps...
  523. //
  524. m_huffLeaves.setSize(256);
  525. m_huffNodes.reserve(256);
  526. m_huffNodes.increment();
  527. for (i = 0; i < 256; i++) {
  528. HuffLeaf& rLeaf = m_huffLeaves[i];
  529. rLeaf.pop = csm_charFreqs[i] + 1;
  530. rLeaf.symbol = U8(i);
  531. dMemset(&rLeaf.code, 0, sizeof(rLeaf.code));
  532. rLeaf.numBits = 0;
  533. }
  534. S32 currWraps = 256;
  535. HuffWrap* pWrap = new HuffWrap[256];
  536. for (i = 0; i < 256; i++) {
  537. pWrap[i].set(&m_huffLeaves[i]);
  538. }
  539. while (currWraps != 1) {
  540. U32 min1 = 0xfffffffe, min2 = 0xffffffff;
  541. S32 index1 = -1, index2 = -1;
  542. for (i = 0; i < currWraps; i++) {
  543. if (pWrap[i].getPop() < min1) {
  544. min2 = min1;
  545. index2 = index1;
  546. min1 = pWrap[i].getPop();
  547. index1 = i;
  548. } else if (pWrap[i].getPop() < min2) {
  549. min2 = pWrap[i].getPop();
  550. index2 = i;
  551. }
  552. }
  553. AssertFatal(index1 != -1 && index2 != -1 && index1 != index2, "hrph");
  554. // Create a node for this...
  555. m_huffNodes.increment();
  556. HuffNode& rNode = m_huffNodes.last();
  557. rNode.pop = pWrap[index1].getPop() + pWrap[index2].getPop();
  558. rNode.index0 = determineIndex(pWrap[index1]);
  559. rNode.index1 = determineIndex(pWrap[index2]);
  560. S32 mergeIndex = index1 > index2 ? index2 : index1;
  561. S32 nukeIndex = index1 > index2 ? index1 : index2;
  562. pWrap[mergeIndex].set(&rNode);
  563. if (index2 != (currWraps - 1)) {
  564. pWrap[nukeIndex] = pWrap[currWraps - 1];
  565. }
  566. currWraps--;
  567. }
  568. AssertFatal(currWraps == 1, "wrong wraps?");
  569. AssertFatal(pWrap[0].pNode != NULL && pWrap[0].pLeaf == NULL, "Wrong wrap type!");
  570. // Ok, now we have one wrap, which is a node. we need to make sure that this
  571. // is the first node in the node list.
  572. m_huffNodes[0] = *(pWrap[0].pNode);
  573. delete [] pWrap;
  574. U32 code = 0;
  575. BitStream bs(&code, 4);
  576. generateCodes(bs, 0, 0);
  577. }
  578. void HuffmanProcessor::generateCodes(BitStream& rBS, S32 index, S32 depth)
  579. {
  580. if (index < 0) {
  581. // leaf node, copy the code in, and back out...
  582. HuffLeaf& rLeaf = m_huffLeaves[-(index + 1)];
  583. dMemcpy(&rLeaf.code, rBS.dataPtr, sizeof(rLeaf.code));
  584. rLeaf.numBits = depth;
  585. } else {
  586. HuffNode& rNode = m_huffNodes[index];
  587. S32 pos = rBS.getCurPos();
  588. rBS.writeFlag(false);
  589. generateCodes(rBS, rNode.index0, depth + 1);
  590. rBS.setCurPos(pos);
  591. rBS.writeFlag(true);
  592. generateCodes(rBS, rNode.index1, depth + 1);
  593. rBS.setCurPos(pos);
  594. }
  595. }
  596. S16 HuffmanProcessor::determineIndex(HuffWrap& rWrap)
  597. {
  598. if (rWrap.pLeaf != NULL) {
  599. AssertFatal(rWrap.pNode == NULL, "Got a non-NULL pNode in a HuffWrap with a non-NULL leaf.");
  600. return -((rWrap.pLeaf - m_huffLeaves.address()) + 1);
  601. } else {
  602. AssertFatal(rWrap.pNode != NULL, "Got a NULL pNode in a HuffWrap with a NULL leaf.");
  603. return rWrap.pNode - m_huffNodes.address();
  604. }
  605. }
  606. bool HuffmanProcessor::readHuffBuffer(BitStream* pStream, char* out_pBuffer)
  607. {
  608. if (m_tablesBuilt == false)
  609. buildTables();
  610. if (pStream->readFlag()) {
  611. S32 len = pStream->readInt(8);
  612. for (S32 i = 0; i < len; i++) {
  613. S32 index = 0;
  614. while (true) {
  615. if (index >= 0) {
  616. if (pStream->readFlag() == true) {
  617. index = m_huffNodes[index].index1;
  618. } else {
  619. index = m_huffNodes[index].index0;
  620. }
  621. } else {
  622. out_pBuffer[i] = m_huffLeaves[-(index+1)].symbol;
  623. break;
  624. }
  625. }
  626. }
  627. out_pBuffer[len] = '\0';
  628. return true;
  629. } else {
  630. // Uncompressed string...
  631. U32 len = pStream->readInt(8);
  632. pStream->read(len, out_pBuffer);
  633. out_pBuffer[len] = '\0';
  634. return true;
  635. }
  636. }
  637. bool HuffmanProcessor::writeHuffBuffer(BitStream* pStream, const char* out_pBuffer, S32 maxLen)
  638. {
  639. if (out_pBuffer == NULL) {
  640. pStream->writeFlag(false);
  641. pStream->writeInt(0, 8);
  642. return true;
  643. }
  644. if (m_tablesBuilt == false)
  645. buildTables();
  646. S32 len = out_pBuffer ? dStrlen(out_pBuffer) : 0;
  647. AssertWarn(len <= 255, "String TOO long for writeString");
  648. AssertWarn(len <= 255, out_pBuffer);
  649. if (len > maxLen)
  650. len = maxLen;
  651. S32 numBits = 0;
  652. S32 i;
  653. for (i = 0; i < len; i++)
  654. numBits += m_huffLeaves[(unsigned char)out_pBuffer[i]].numBits;
  655. if (numBits >= (len * 8)) {
  656. pStream->writeFlag(false);
  657. pStream->writeInt(len, 8);
  658. pStream->write(len, out_pBuffer);
  659. } else {
  660. pStream->writeFlag(true);
  661. pStream->writeInt(len, 8);
  662. for (i = 0; i < len; i++) {
  663. HuffLeaf& rLeaf = m_huffLeaves[((unsigned char)out_pBuffer[i])];
  664. pStream->writeBits(rLeaf.numBits, &rLeaf.code);
  665. }
  666. }
  667. return true;
  668. }
  669. const U32 HuffmanProcessor::csm_charFreqs[256] = {
  670. 0 ,
  671. 0 ,
  672. 0 ,
  673. 0 ,
  674. 0 ,
  675. 0 ,
  676. 0 ,
  677. 0 ,
  678. 0 ,
  679. 329 ,
  680. 21 ,
  681. 0 ,
  682. 0 ,
  683. 0 ,
  684. 0 ,
  685. 0 ,
  686. 0 ,
  687. 0 ,
  688. 0 ,
  689. 0 ,
  690. 0 ,
  691. 0 ,
  692. 0 ,
  693. 0 ,
  694. 0 ,
  695. 0 ,
  696. 0 ,
  697. 0 ,
  698. 0 ,
  699. 0 ,
  700. 0 ,
  701. 0 ,
  702. 2809 ,
  703. 68 ,
  704. 0 ,
  705. 27 ,
  706. 0 ,
  707. 58 ,
  708. 3 ,
  709. 62 ,
  710. 4 ,
  711. 7 ,
  712. 0 ,
  713. 0 ,
  714. 15 ,
  715. 65 ,
  716. 554 ,
  717. 3 ,
  718. 394 ,
  719. 404 ,
  720. 189 ,
  721. 117 ,
  722. 30 ,
  723. 51 ,
  724. 27 ,
  725. 15 ,
  726. 34 ,
  727. 32 ,
  728. 80 ,
  729. 1 ,
  730. 142 ,
  731. 3 ,
  732. 142 ,
  733. 39 ,
  734. 0 ,
  735. 144 ,
  736. 125 ,
  737. 44 ,
  738. 122 ,
  739. 275 ,
  740. 70 ,
  741. 135 ,
  742. 61 ,
  743. 127 ,
  744. 8 ,
  745. 12 ,
  746. 113 ,
  747. 246 ,
  748. 122 ,
  749. 36 ,
  750. 185 ,
  751. 1 ,
  752. 149 ,
  753. 309 ,
  754. 335 ,
  755. 12 ,
  756. 11 ,
  757. 14 ,
  758. 54 ,
  759. 151 ,
  760. 0 ,
  761. 0 ,
  762. 2 ,
  763. 0 ,
  764. 0 ,
  765. 211 ,
  766. 0 ,
  767. 2090 ,
  768. 344 ,
  769. 736 ,
  770. 993 ,
  771. 2872 ,
  772. 701 ,
  773. 605 ,
  774. 646 ,
  775. 1552 ,
  776. 328 ,
  777. 305 ,
  778. 1240 ,
  779. 735 ,
  780. 1533 ,
  781. 1713 ,
  782. 562 ,
  783. 3 ,
  784. 1775 ,
  785. 1149 ,
  786. 1469 ,
  787. 979 ,
  788. 407 ,
  789. 553 ,
  790. 59 ,
  791. 279 ,
  792. 31 ,
  793. 0 ,
  794. 0 ,
  795. 0 ,
  796. 68 ,
  797. 0 ,
  798. 0 ,
  799. 0 ,
  800. 0 ,
  801. 0 ,
  802. 0 ,
  803. 0 ,
  804. 0 ,
  805. 0 ,
  806. 0 ,
  807. 0 ,
  808. 0 ,
  809. 0 ,
  810. 0 ,
  811. 0 ,
  812. 0 ,
  813. 0 ,
  814. 0 ,
  815. 0 ,
  816. 0 ,
  817. 0 ,
  818. 0 ,
  819. 0 ,
  820. 0 ,
  821. 0 ,
  822. 0 ,
  823. 0 ,
  824. 0 ,
  825. 0 ,
  826. 0 ,
  827. 0 ,
  828. 0 ,
  829. 0 ,
  830. 0 ,
  831. 0 ,
  832. 0 ,
  833. 0 ,
  834. 0 ,
  835. 0 ,
  836. 0 ,
  837. 0 ,
  838. 0 ,
  839. 0 ,
  840. 0 ,
  841. 0 ,
  842. 0 ,
  843. 0 ,
  844. 0 ,
  845. 0 ,
  846. 0 ,
  847. 0 ,
  848. 0 ,
  849. 0 ,
  850. 0 ,
  851. 0 ,
  852. 0 ,
  853. 0 ,
  854. 0 ,
  855. 0 ,
  856. 0 ,
  857. 0 ,
  858. 0 ,
  859. 0 ,
  860. 0 ,
  861. 0 ,
  862. 0 ,
  863. 0 ,
  864. 0 ,
  865. 0 ,
  866. 0 ,
  867. 0 ,
  868. 0 ,
  869. 0 ,
  870. 0 ,
  871. 0 ,
  872. 0 ,
  873. 0 ,
  874. 0 ,
  875. 0 ,
  876. 0 ,
  877. 0 ,
  878. 0 ,
  879. 0 ,
  880. 0 ,
  881. 0 ,
  882. 0 ,
  883. 0 ,
  884. 0 ,
  885. 0 ,
  886. 0 ,
  887. 0 ,
  888. 0 ,
  889. 0 ,
  890. 0 ,
  891. 0 ,
  892. 0 ,
  893. 0 ,
  894. 0 ,
  895. 0 ,
  896. 0 ,
  897. 0 ,
  898. 0 ,
  899. 0 ,
  900. 0 ,
  901. 0 ,
  902. 0 ,
  903. 0 ,
  904. 0 ,
  905. 0 ,
  906. 0 ,
  907. 0 ,
  908. 0 ,
  909. 0 ,
  910. 0 ,
  911. 0 ,
  912. 0 ,
  913. 0 ,
  914. 0 ,
  915. 0 ,
  916. 0 ,
  917. 0 ,
  918. 0 ,
  919. 0 ,
  920. 0 ,
  921. 0 ,
  922. 0 ,
  923. 0 ,
  924. 0 ,
  925. 0
  926. };