ImfFastHuf.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2009-2014 DreamWorks Animation LLC.
  4. //
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of DreamWorks Animation nor the names of
  17. // its contributors may be used to endorse or promote products derived
  18. // from this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. //
  32. ///////////////////////////////////////////////////////////////////////////
  33. #include "ImfFastHuf.h"
  34. #include <Iex.h>
  35. #include <string.h>
  36. #include <assert.h>
  37. #include <math.h>
  38. #include <vector>
  39. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  40. //
  41. // Adapted from hufUnpackEncTable -
  42. // We don't need to reconstruct the code book, just the encoded
  43. // lengths for each symbol. From the lengths, we can build the
  44. // base + offset tables. This should be a bit more efficient
  45. // for sparse code books.
  46. //
  47. // table - ptr to the start of the code length data. Will be
  48. // updated as we decode data
  49. //
  50. // numBytes - size of the encoded table (I think)?
  51. //
  52. // minSymbol - smallest symbol in the code book
  53. //
  54. // maxSymbol - largest symbol in the code book.
  55. //
  56. // rleSymbol - the symbol to trigger RLE in the encoded bitstream
  57. //
  58. FastHufDecoder::FastHufDecoder
  59. (const char *&table,
  60. int numBytes,
  61. int minSymbol,
  62. int maxSymbol,
  63. int rleSymbol)
  64. :
  65. _rleSymbol (rleSymbol),
  66. _numSymbols (0),
  67. _minCodeLength (255),
  68. _maxCodeLength (0),
  69. _idToSymbol (0)
  70. {
  71. //
  72. // List of symbols that we find with non-zero code lengths
  73. // (listed in the order we find them). Store these in the
  74. // same format as the code book stores codes + lengths -
  75. // low 6 bits are the length, everything above that is
  76. // the symbol.
  77. //
  78. std::vector<Int64> symbols;
  79. //
  80. // The 'base' table is the minimum code at each code length. base[i]
  81. // is the smallest code (numerically) of length i.
  82. //
  83. Int64 base[MAX_CODE_LEN + 1];
  84. //
  85. // The 'offset' table is the position (in sorted order) of the first id
  86. // of a given code lenght. Array is indexed by code length, like base.
  87. //
  88. Int64 offset[MAX_CODE_LEN + 1];
  89. //
  90. // Count of how many codes at each length there are. Array is
  91. // indexed by code length, like base and offset.
  92. //
  93. size_t codeCount[MAX_CODE_LEN + 1];
  94. for (int i = 0; i <= MAX_CODE_LEN; ++i)
  95. {
  96. codeCount[i] = 0;
  97. base[i] = 0xffffffffffffffffULL;
  98. offset[i] = 0;
  99. }
  100. //
  101. // Count the number of codes, the min/max code lengths, the number of
  102. // codes with each length, and record symbols with non-zero code
  103. // length as we find them.
  104. //
  105. const char *currByte = table;
  106. Int64 currBits = 0;
  107. int currBitCount = 0;
  108. const int SHORT_ZEROCODE_RUN = 59;
  109. const int LONG_ZEROCODE_RUN = 63;
  110. const int SHORTEST_LONG_RUN = 2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN;
  111. for (Int64 symbol = minSymbol; symbol <= maxSymbol; symbol++)
  112. {
  113. if (currByte - table > numBytes)
  114. {
  115. throw IEX_NAMESPACE::InputExc ("Error decoding Huffman table "
  116. "(Truncated table data).");
  117. }
  118. //
  119. // Next code length - either:
  120. // 0-58 (literal code length)
  121. // 59-62 (various lengths runs of 0)
  122. // 63 (run of n 0's, with n is the next 8 bits)
  123. //
  124. Int64 codeLen = readBits (6, currBits, currBitCount, currByte);
  125. if (codeLen == (Int64) LONG_ZEROCODE_RUN)
  126. {
  127. if (currByte - table > numBytes)
  128. {
  129. throw IEX_NAMESPACE::InputExc ("Error decoding Huffman table "
  130. "(Truncated table data).");
  131. }
  132. int runLen = readBits (8, currBits, currBitCount, currByte) +
  133. SHORTEST_LONG_RUN;
  134. if (symbol + runLen > maxSymbol + 1)
  135. {
  136. throw IEX_NAMESPACE::InputExc ("Error decoding Huffman table "
  137. "(Run beyond end of table).");
  138. }
  139. symbol += runLen - 1;
  140. }
  141. else if (codeLen >= (Int64) SHORT_ZEROCODE_RUN)
  142. {
  143. int runLen = codeLen - SHORT_ZEROCODE_RUN + 2;
  144. if (symbol + runLen > maxSymbol + 1)
  145. {
  146. throw IEX_NAMESPACE::InputExc ("Error decoding Huffman table "
  147. "(Run beyond end of table).");
  148. }
  149. symbol += runLen - 1;
  150. }
  151. else if (codeLen != 0)
  152. {
  153. symbols.push_back ((symbol << 6) | (codeLen & 63));
  154. if (codeLen < _minCodeLength)
  155. _minCodeLength = codeLen;
  156. if (codeLen > _maxCodeLength)
  157. _maxCodeLength = codeLen;
  158. codeCount[codeLen]++;
  159. }
  160. }
  161. for (int i = 0; i < MAX_CODE_LEN; ++i)
  162. _numSymbols += codeCount[i];
  163. table = currByte;
  164. //
  165. // Compute base - once we have the code length counts, there
  166. // is a closed form solution for this
  167. //
  168. {
  169. double* countTmp = new double[_maxCodeLength+1];
  170. for (int l = _minCodeLength; l <= _maxCodeLength; ++l)
  171. {
  172. countTmp[l] = (double)codeCount[l] *
  173. (double)(2 << (_maxCodeLength-l));
  174. }
  175. for (int l = _minCodeLength; l <= _maxCodeLength; ++l)
  176. {
  177. double tmp = 0;
  178. for (int k =l + 1; k <= _maxCodeLength; ++k)
  179. tmp += countTmp[k];
  180. tmp /= (double)(2 << (_maxCodeLength - l));
  181. base[l] = (Int64)ceil (tmp);
  182. }
  183. delete [] countTmp;
  184. }
  185. //
  186. // Compute offset - these are the positions of the first
  187. // id (not symbol) that has length [i]
  188. //
  189. offset[_maxCodeLength] = 0;
  190. for (int i= _maxCodeLength - 1; i >= _minCodeLength; i--)
  191. offset[i] = offset[i + 1] + codeCount[i + 1];
  192. //
  193. // Allocate and fill the symbol-to-id mapping. Smaller Ids should be
  194. // mapped to less-frequent symbols (which have longer codes). Use
  195. // the offset table to tell us where the id's for a given code
  196. // length start off.
  197. //
  198. _idToSymbol = new int[_numSymbols];
  199. Int64 mapping[MAX_CODE_LEN + 1];
  200. for (int i = 0; i < MAX_CODE_LEN + 1; ++i)
  201. mapping[i] = -1;
  202. for (int i = _minCodeLength; i <= _maxCodeLength; ++i)
  203. mapping[i] = offset[i];
  204. for (std::vector<Int64>::const_iterator i = symbols.begin();
  205. i != symbols.end();
  206. ++i)
  207. {
  208. int codeLen = *i & 63;
  209. int symbol = *i >> 6;
  210. if (mapping[codeLen] >= _numSymbols)
  211. throw IEX_NAMESPACE::InputExc ("Huffman decode error "
  212. "(Invalid symbol in header).");
  213. _idToSymbol[mapping[codeLen]] = symbol;
  214. mapping[codeLen]++;
  215. }
  216. buildTables(base, offset);
  217. }
  218. FastHufDecoder::~FastHufDecoder()
  219. {
  220. delete[] _idToSymbol;
  221. }
  222. //
  223. // Static check if the decoder is enabled.
  224. //
  225. // ATM, I only have access to little endian hardware for testing,
  226. // so I'm not entirely sure that we are reading fom the bit stream
  227. // properly on BE.
  228. //
  229. // If you happen to have more obscure hardware, check that the
  230. // byte swapping in refill() is happening sensable, add an endian
  231. // check if needed, and fix the preprocessor magic here.
  232. //
  233. #define READ64(c) \
  234. ((Int64)(c)[0] << 56) | ((Int64)(c)[1] << 48) | ((Int64)(c)[2] << 40) | \
  235. ((Int64)(c)[3] << 32) | ((Int64)(c)[4] << 24) | ((Int64)(c)[5] << 16) | \
  236. ((Int64)(c)[6] << 8) | ((Int64)(c)[7] )
  237. #ifdef __INTEL_COMPILER // ICC built-in swap for LE hosts
  238. #if defined (__i386__) || defined(__x86_64__)
  239. #undef READ64
  240. #define READ64(c) _bswap64 (*(const Int64*)(c))
  241. #endif
  242. #endif
  243. bool
  244. FastHufDecoder::enabled()
  245. {
  246. #if defined(__INTEL_COMPILER) || defined(__GNUC__)
  247. //
  248. // Enabled for ICC, GCC:
  249. // __i386__ -> x86
  250. // __x86_64__ -> 64-bit x86
  251. //
  252. #if defined (__i386__) || defined(__x86_64__)
  253. return true;
  254. #else
  255. return false;
  256. #endif
  257. #elif defined (_MSC_VER)
  258. //
  259. // Enabled for Visual Studio:
  260. // _M_IX86 -> x86
  261. // _M_X64 -> 64bit x86
  262. #if defined (_M_IX86) || defined(_M_X64)
  263. return true;
  264. #else
  265. return false;
  266. #endif
  267. #else
  268. //
  269. // Unknown compiler - Be safe and disable.
  270. //
  271. return false;
  272. #endif
  273. }
  274. //
  275. //
  276. // Built the acceleration tables for lookups on the upper bits
  277. // as well as the 'LJ' tables.
  278. //
  279. void
  280. FastHufDecoder::buildTables (Int64 *base, Int64 *offset)
  281. {
  282. //
  283. // Build the 'left justified' base table, by shifting base left..
  284. //
  285. for (int i = 0; i <= MAX_CODE_LEN; ++i)
  286. {
  287. if (base[i] != 0xffffffffffffffffULL)
  288. {
  289. _ljBase[i] = base[i] << (64 - i);
  290. }
  291. else
  292. {
  293. //
  294. // Unused code length - insert dummy values
  295. //
  296. _ljBase[i] = 0xffffffffffffffffULL;
  297. }
  298. }
  299. //
  300. // Combine some terms into a big fat constant, which for
  301. // lack of a better term we'll call the 'left justified'
  302. // offset table (because it serves the same function
  303. // as 'offset', when using the left justified base table.
  304. //
  305. for (int i = 0; i <= MAX_CODE_LEN; ++i)
  306. _ljOffset[i] = offset[i] - (_ljBase[i] >> (64 - i));
  307. //
  308. // Build the acceleration tables for the lookups of
  309. // short codes ( <= TABLE_LOOKUP_BITS long)
  310. //
  311. for (Int64 i = 0; i < 1 << TABLE_LOOKUP_BITS; ++i)
  312. {
  313. Int64 value = i << (64 - TABLE_LOOKUP_BITS);
  314. _tableSymbol[i] = 0xffff;
  315. _tableCodeLen[i] = 0;
  316. for (int codeLen = _minCodeLength; codeLen <= _maxCodeLength; ++codeLen)
  317. {
  318. if (_ljBase[codeLen] <= value)
  319. {
  320. _tableCodeLen[i] = codeLen;
  321. Int64 id = _ljOffset[codeLen] + (value >> (64 - codeLen));
  322. if (id < _numSymbols)
  323. {
  324. _tableSymbol[i] = _idToSymbol[id];
  325. }
  326. else
  327. {
  328. throw IEX_NAMESPACE::InputExc ("Huffman decode error "
  329. "(Overrun).");
  330. }
  331. break;
  332. }
  333. }
  334. }
  335. //
  336. // Store the smallest value in the table that points to real data.
  337. // This should be the entry for the largest length that has
  338. // valid data (in our case, non-dummy _ljBase)
  339. //
  340. int minIdx = TABLE_LOOKUP_BITS;
  341. while (minIdx > 0 && _ljBase[minIdx] == 0xffffffffffffffffULL)
  342. minIdx--;
  343. if (minIdx < 0)
  344. {
  345. //
  346. // Error, no codes with lengths 0-TABLE_LOOKUP_BITS used.
  347. // Set the min value such that the table is never tested.
  348. //
  349. _tableMin = 0xffffffffffffffffULL;
  350. }
  351. else
  352. {
  353. _tableMin = _ljBase[minIdx];
  354. }
  355. }
  356. //
  357. // For decoding, we're holding onto 2 Int64's.
  358. //
  359. // The first (buffer), holds the next bits from the bitstream to be
  360. // decoded. For certain paths in the decoder, we only need TABLE_LOOKUP_BITS
  361. // valid bits to decode the next symbol. For other paths, we need a full
  362. // 64-bits to decode a symbol.
  363. //
  364. // When we need to refill 'buffer', we could pull bits straight from
  365. // the bitstream. But this is very slow and requires lots of book keeping
  366. // (what's the next bit in the next byte?). Instead, we keep another Int64
  367. // around that we use to refill from. While this doesn't cut down on the
  368. // book keeping (still need to know how many valid bits), it does cut
  369. // down on some of the bit shifting crazy and byte access.
  370. //
  371. // The refill Int64 (bufferBack) gets left-shifted after we've pulled
  372. // off bits. If we run out of bits in the input bit stream, we just
  373. // shift in 0's to bufferBack.
  374. //
  375. // The refill act takes numBits from the top of bufferBack and sticks
  376. // them in the bottom of buffer. If there arn't enough bits in bufferBack,
  377. // it gets refilled (to 64-bits) from the input bitstream.
  378. //
  379. inline void
  380. FastHufDecoder::refill
  381. (Int64 &buffer,
  382. int numBits, // number of bits to refill
  383. Int64 &bufferBack, // the next 64-bits, to refill from
  384. int &bufferBackNumBits, // number of bits left in bufferBack
  385. const unsigned char *&currByte, // current byte in the bitstream
  386. int &currBitsLeft) // number of bits left in the bitsream
  387. {
  388. //
  389. // Refill bits into the bottom of buffer, from the top of bufferBack.
  390. // Always top up buffer to be completely full.
  391. //
  392. buffer |= bufferBack >> (64 - numBits);
  393. if (bufferBackNumBits < numBits)
  394. {
  395. numBits -= bufferBackNumBits;
  396. //
  397. // Refill all of bufferBack from the bitstream. Either grab
  398. // a full 64-bit chunk, or whatever bytes are left. If we
  399. // don't have 64-bits left, pad with 0's.
  400. //
  401. if (currBitsLeft >= 64)
  402. {
  403. bufferBack = READ64 (currByte);
  404. bufferBackNumBits = 64;
  405. currByte += sizeof (Int64);
  406. currBitsLeft -= 8 * sizeof (Int64);
  407. }
  408. else
  409. {
  410. bufferBack = 0;
  411. bufferBackNumBits = 64;
  412. Int64 shift = 56;
  413. while (currBitsLeft > 0)
  414. {
  415. bufferBack |= ((Int64)(*currByte)) << shift;
  416. currByte++;
  417. shift -= 8;
  418. currBitsLeft -= 8;
  419. }
  420. //
  421. // At this point, currBitsLeft might be negative, just because
  422. // we're subtracting whole bytes. To keep anyone from freaking
  423. // out, zero the counter.
  424. //
  425. if (currBitsLeft < 0)
  426. currBitsLeft = 0;
  427. }
  428. buffer |= bufferBack >> (64 - numBits);
  429. }
  430. bufferBack = bufferBack << numBits;
  431. bufferBackNumBits -= numBits;
  432. //
  433. // We can have cases where the previous shift of bufferBack is << 64 -
  434. // in which case no shift occurs. The bit count math still works though,
  435. // so if we don't have any bits left, zero out bufferBack.
  436. //
  437. if (bufferBackNumBits == 0)
  438. bufferBack = 0;
  439. }
  440. //
  441. // Read the next few bits out of a bitstream. Will be given a backing buffer
  442. // (buffer) that may still have data left over from previous reads
  443. // (bufferNumBits). Bitstream pointer (currByte) will be advanced when needed.
  444. //
  445. inline Int64
  446. FastHufDecoder::readBits
  447. (int numBits,
  448. Int64 &buffer, // c
  449. int &bufferNumBits, // lc
  450. const char *&currByte) // in
  451. {
  452. while (bufferNumBits < numBits)
  453. {
  454. buffer = (buffer << 8) | *(unsigned char*)(currByte++);
  455. bufferNumBits += 8;
  456. }
  457. bufferNumBits -= numBits;
  458. return (buffer >> bufferNumBits) & ((1 << numBits) - 1);
  459. }
  460. //
  461. // Decode using a the 'One-Shift' strategy for decoding, with a
  462. // small-ish table to accelerate decoding of short codes.
  463. //
  464. // If possible, try looking up codes into the acceleration table.
  465. // This has a few benifits - there's no search involved; We don't
  466. // need an additional lookup to map id to symbol; we don't need
  467. // a full 64-bits (so less refilling).
  468. //
  469. void
  470. FastHufDecoder::decode
  471. (const unsigned char *src,
  472. int numSrcBits,
  473. unsigned short *dst,
  474. int numDstElems)
  475. {
  476. if (numSrcBits < 128)
  477. throw IEX_NAMESPACE::InputExc ("Error choosing Huffman decoder implementation "
  478. "(insufficient number of bits).");
  479. //
  480. // Current position (byte/bit) in the src data stream
  481. // (after the first buffer fill)
  482. //
  483. const unsigned char *currByte = src + 2 * sizeof (Int64);
  484. numSrcBits -= 8 * 2 * sizeof (Int64);
  485. //
  486. // 64-bit buffer holding the current bits in the stream
  487. //
  488. Int64 buffer = READ64 (src);
  489. int bufferNumBits = 64;
  490. //
  491. // 64-bit buffer holding the next bits in the stream
  492. //
  493. Int64 bufferBack = READ64 ((src + sizeof (Int64)));
  494. int bufferBackNumBits = 64;
  495. int dstIdx = 0;
  496. while (dstIdx < numDstElems)
  497. {
  498. int codeLen;
  499. int symbol;
  500. //
  501. // Test if we can be table accelerated. If so, directly
  502. // lookup the output symbol. Otherwise, we need to fall
  503. // back to searching for the code.
  504. //
  505. // If we're doing table lookups, we don't really need
  506. // a re-filled buffer, so long as we have TABLE_LOOKUP_BITS
  507. // left. But for a search, we do need a refilled table.
  508. //
  509. if (_tableMin <= buffer)
  510. {
  511. int tableIdx = buffer >> (64 - TABLE_LOOKUP_BITS);
  512. //
  513. // For invalid codes, _tableCodeLen[] should return 0. This
  514. // will cause the decoder to get stuck in the current spot
  515. // until we run out of elements, then barf that the codestream
  516. // is bad. So we don't need to stick a condition like
  517. // if (codeLen > _maxCodeLength) in this inner.
  518. //
  519. codeLen = _tableCodeLen[tableIdx];
  520. symbol = _tableSymbol[tableIdx];
  521. }
  522. else
  523. {
  524. if (bufferNumBits < 64)
  525. {
  526. refill (buffer,
  527. 64 - bufferNumBits,
  528. bufferBack,
  529. bufferBackNumBits,
  530. currByte,
  531. numSrcBits);
  532. bufferNumBits = 64;
  533. }
  534. //
  535. // Brute force search:
  536. // Find the smallest length where _ljBase[length] <= buffer
  537. //
  538. codeLen = TABLE_LOOKUP_BITS + 1;
  539. while (_ljBase[codeLen] > buffer && codeLen <= _maxCodeLength)
  540. codeLen++;
  541. if (codeLen > _maxCodeLength)
  542. {
  543. throw IEX_NAMESPACE::InputExc ("Huffman decode error "
  544. "(Decoded an invalid symbol).");
  545. }
  546. Int64 id = _ljOffset[codeLen] + (buffer >> (64 - codeLen));
  547. if (id < _numSymbols)
  548. {
  549. symbol = _idToSymbol[id];
  550. }
  551. else
  552. {
  553. throw IEX_NAMESPACE::InputExc ("Huffman decode error "
  554. "(Decoded an invalid symbol).");
  555. }
  556. }
  557. //
  558. // Shift over bit stream, and update the bit count in the buffer
  559. //
  560. buffer = buffer << codeLen;
  561. bufferNumBits -= codeLen;
  562. //
  563. // If we recieved a RLE symbol (_rleSymbol), then we need
  564. // to read ahead 8 bits to know how many times to repeat
  565. // the previous symbol. Need to ensure we at least have
  566. // 8 bits of data in the buffer
  567. //
  568. if (symbol == _rleSymbol)
  569. {
  570. if (bufferNumBits < 8)
  571. {
  572. refill (buffer,
  573. 64 - bufferNumBits,
  574. bufferBack,
  575. bufferBackNumBits,
  576. currByte,
  577. numSrcBits);
  578. bufferNumBits = 64;
  579. }
  580. int rleCount = buffer >> 56;
  581. if (dstIdx < 1)
  582. {
  583. throw IEX_NAMESPACE::InputExc ("Huffman decode error (RLE code "
  584. "with no previous symbol).");
  585. }
  586. if (dstIdx + rleCount > numDstElems)
  587. {
  588. throw IEX_NAMESPACE::InputExc ("Huffman decode error (Symbol run "
  589. "beyond expected output buffer length).");
  590. }
  591. if (rleCount <= 0)
  592. {
  593. throw IEX_NAMESPACE::InputExc("Huffman decode error"
  594. " (Invalid RLE length)");
  595. }
  596. for (int i = 0; i < rleCount; ++i)
  597. dst[dstIdx + i] = dst[dstIdx - 1];
  598. dstIdx += rleCount;
  599. buffer = buffer << 8;
  600. bufferNumBits -= 8;
  601. }
  602. else
  603. {
  604. dst[dstIdx] = symbol;
  605. dstIdx++;
  606. }
  607. //
  608. // refill bit stream buffer if we're below the number of
  609. // bits needed for a table lookup
  610. //
  611. if (bufferNumBits < TABLE_LOOKUP_BITS)
  612. {
  613. refill (buffer,
  614. 64 - bufferNumBits,
  615. bufferBack,
  616. bufferBackNumBits,
  617. currByte,
  618. numSrcBits);
  619. bufferNumBits = 64;
  620. }
  621. }
  622. if (numSrcBits != 0)
  623. {
  624. throw IEX_NAMESPACE::InputExc ("Huffman decode error (Compressed data remains "
  625. "after filling expected output buffer).");
  626. }
  627. }
  628. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT