ImfHuf.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. //-----------------------------------------------------------------------------
  35. //
  36. // 16-bit Huffman compression and decompression.
  37. //
  38. // The source code in this file is derived from the 8-bit
  39. // Huffman compression and decompression routines written
  40. // by Christian Rouet for his PIZ image file format.
  41. //
  42. //-----------------------------------------------------------------------------
  43. #include <ImfHuf.h>
  44. #include <ImfInt64.h>
  45. #include "ImfAutoArray.h"
  46. #include "ImfFastHuf.h"
  47. #include "Iex.h"
  48. #include <cstring>
  49. #include <cassert>
  50. #include <algorithm>
  51. using namespace std;
  52. using namespace IEX_NAMESPACE;
  53. #include "ImfNamespace.h"
  54. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  55. namespace {
  56. const int HUF_ENCBITS = 16; // literal (value) bit length
  57. const int HUF_DECBITS = 14; // decoding bit size (>= 8)
  58. const int HUF_ENCSIZE = (1 << HUF_ENCBITS) + 1; // encoding table size
  59. const int HUF_DECSIZE = 1 << HUF_DECBITS; // decoding table size
  60. const int HUF_DECMASK = HUF_DECSIZE - 1;
  61. struct HufDec
  62. { // short code long code
  63. //-------------------------------
  64. int len:8; // code length 0
  65. int lit:24; // lit p size
  66. int * p; // 0 lits
  67. };
  68. void
  69. invalidNBits ()
  70. {
  71. throw InputExc ("Error in header for Huffman-encoded data "
  72. "(invalid number of bits).");
  73. }
  74. void
  75. tooMuchData ()
  76. {
  77. throw InputExc ("Error in Huffman-encoded data "
  78. "(decoded data are longer than expected).");
  79. }
  80. void
  81. notEnoughData ()
  82. {
  83. throw InputExc ("Error in Huffman-encoded data "
  84. "(decoded data are shorter than expected).");
  85. }
  86. void
  87. invalidCode ()
  88. {
  89. throw InputExc ("Error in Huffman-encoded data "
  90. "(invalid code).");
  91. }
  92. void
  93. invalidTableSize ()
  94. {
  95. throw InputExc ("Error in Huffman-encoded data "
  96. "(invalid code table size).");
  97. }
  98. void
  99. unexpectedEndOfTable ()
  100. {
  101. throw InputExc ("Error in Huffman-encoded data "
  102. "(unexpected end of code table data).");
  103. }
  104. void
  105. tableTooLong ()
  106. {
  107. throw InputExc ("Error in Huffman-encoded data "
  108. "(code table is longer than expected).");
  109. }
  110. void
  111. invalidTableEntry ()
  112. {
  113. throw InputExc ("Error in Huffman-encoded data "
  114. "(invalid code table entry).");
  115. }
  116. inline Int64
  117. hufLength (Int64 code)
  118. {
  119. return code & 63;
  120. }
  121. inline Int64
  122. hufCode (Int64 code)
  123. {
  124. return code >> 6;
  125. }
  126. inline void
  127. outputBits (int nBits, Int64 bits, Int64 &c, int &lc, char *&out)
  128. {
  129. c <<= nBits;
  130. lc += nBits;
  131. c |= bits;
  132. while (lc >= 8)
  133. *out++ = (c >> (lc -= 8));
  134. }
  135. inline Int64
  136. getBits (int nBits, Int64 &c, int &lc, const char *&in)
  137. {
  138. while (lc < nBits)
  139. {
  140. c = (c << 8) | *(unsigned char *)(in++);
  141. lc += 8;
  142. }
  143. lc -= nBits;
  144. return (c >> lc) & ((1 << nBits) - 1);
  145. }
  146. //
  147. // ENCODING TABLE BUILDING & (UN)PACKING
  148. //
  149. //
  150. // Build a "canonical" Huffman code table:
  151. // - for each (uncompressed) symbol, hcode contains the length
  152. // of the corresponding code (in the compressed data)
  153. // - canonical codes are computed and stored in hcode
  154. // - the rules for constructing canonical codes are as follows:
  155. // * shorter codes (if filled with zeroes to the right)
  156. // have a numerically higher value than longer codes
  157. // * for codes with the same length, numerical values
  158. // increase with numerical symbol values
  159. // - because the canonical code table can be constructed from
  160. // symbol lengths alone, the code table can be transmitted
  161. // without sending the actual code values
  162. // - see http://www.compressconsult.com/huffman/
  163. //
  164. void
  165. hufCanonicalCodeTable (Int64 hcode[HUF_ENCSIZE])
  166. {
  167. Int64 n[59];
  168. //
  169. // For each i from 0 through 58, count the
  170. // number of different codes of length i, and
  171. // store the count in n[i].
  172. //
  173. for (int i = 0; i <= 58; ++i)
  174. n[i] = 0;
  175. for (int i = 0; i < HUF_ENCSIZE; ++i)
  176. n[hcode[i]] += 1;
  177. //
  178. // For each i from 58 through 1, compute the
  179. // numerically lowest code with length i, and
  180. // store that code in n[i].
  181. //
  182. Int64 c = 0;
  183. for (int i = 58; i > 0; --i)
  184. {
  185. Int64 nc = ((c + n[i]) >> 1);
  186. n[i] = c;
  187. c = nc;
  188. }
  189. //
  190. // hcode[i] contains the length, l, of the
  191. // code for symbol i. Assign the next available
  192. // code of length l to the symbol and store both
  193. // l and the code in hcode[i].
  194. //
  195. for (int i = 0; i < HUF_ENCSIZE; ++i)
  196. {
  197. int l = hcode[i];
  198. if (l > 0)
  199. hcode[i] = l | (n[l]++ << 6);
  200. }
  201. }
  202. //
  203. // Compute Huffman codes (based on frq input) and store them in frq:
  204. // - code structure is : [63:lsb - 6:msb] | [5-0: bit length];
  205. // - max code length is 58 bits;
  206. // - codes outside the range [im-iM] have a null length (unused values);
  207. // - original frequencies are destroyed;
  208. // - encoding tables are used by hufEncode() and hufBuildDecTable();
  209. //
  210. struct FHeapCompare
  211. {
  212. bool operator () (Int64 *a, Int64 *b) {return *a > *b;}
  213. };
  214. void
  215. hufBuildEncTable
  216. (Int64* frq, // io: input frequencies [HUF_ENCSIZE], output table
  217. int* im, // o: min frq index
  218. int* iM) // o: max frq index
  219. {
  220. //
  221. // This function assumes that when it is called, array frq
  222. // indicates the frequency of all possible symbols in the data
  223. // that are to be Huffman-encoded. (frq[i] contains the number
  224. // of occurrences of symbol i in the data.)
  225. //
  226. // The loop below does three things:
  227. //
  228. // 1) Finds the minimum and maximum indices that point
  229. // to non-zero entries in frq:
  230. //
  231. // frq[im] != 0, and frq[i] == 0 for all i < im
  232. // frq[iM] != 0, and frq[i] == 0 for all i > iM
  233. //
  234. // 2) Fills array fHeap with pointers to all non-zero
  235. // entries in frq.
  236. //
  237. // 3) Initializes array hlink such that hlink[i] == i
  238. // for all array entries.
  239. //
  240. AutoArray <int, HUF_ENCSIZE> hlink;
  241. AutoArray <Int64 *, HUF_ENCSIZE> fHeap;
  242. *im = 0;
  243. while (!frq[*im])
  244. (*im)++;
  245. int nf = 0;
  246. for (int i = *im; i < HUF_ENCSIZE; i++)
  247. {
  248. hlink[i] = i;
  249. if (frq[i])
  250. {
  251. fHeap[nf] = &frq[i];
  252. nf++;
  253. *iM = i;
  254. }
  255. }
  256. //
  257. // Add a pseudo-symbol, with a frequency count of 1, to frq;
  258. // adjust the fHeap and hlink array accordingly. Function
  259. // hufEncode() uses the pseudo-symbol for run-length encoding.
  260. //
  261. (*iM)++;
  262. frq[*iM] = 1;
  263. fHeap[nf] = &frq[*iM];
  264. nf++;
  265. //
  266. // Build an array, scode, such that scode[i] contains the number
  267. // of bits assigned to symbol i. Conceptually this is done by
  268. // constructing a tree whose leaves are the symbols with non-zero
  269. // frequency:
  270. //
  271. // Make a heap that contains all symbols with a non-zero frequency,
  272. // with the least frequent symbol on top.
  273. //
  274. // Repeat until only one symbol is left on the heap:
  275. //
  276. // Take the two least frequent symbols off the top of the heap.
  277. // Create a new node that has first two nodes as children, and
  278. // whose frequency is the sum of the frequencies of the first
  279. // two nodes. Put the new node back into the heap.
  280. //
  281. // The last node left on the heap is the root of the tree. For each
  282. // leaf node, the distance between the root and the leaf is the length
  283. // of the code for the corresponding symbol.
  284. //
  285. // The loop below doesn't actually build the tree; instead we compute
  286. // the distances of the leaves from the root on the fly. When a new
  287. // node is added to the heap, then that node's descendants are linked
  288. // into a single linear list that starts at the new node, and the code
  289. // lengths of the descendants (that is, their distance from the root
  290. // of the tree) are incremented by one.
  291. //
  292. make_heap (&fHeap[0], &fHeap[nf], FHeapCompare());
  293. AutoArray <Int64, HUF_ENCSIZE> scode;
  294. memset (scode, 0, sizeof (Int64) * HUF_ENCSIZE);
  295. while (nf > 1)
  296. {
  297. //
  298. // Find the indices, mm and m, of the two smallest non-zero frq
  299. // values in fHeap, add the smallest frq to the second-smallest
  300. // frq, and remove the smallest frq value from fHeap.
  301. //
  302. int mm = fHeap[0] - frq;
  303. pop_heap (&fHeap[0], &fHeap[nf], FHeapCompare());
  304. --nf;
  305. int m = fHeap[0] - frq;
  306. pop_heap (&fHeap[0], &fHeap[nf], FHeapCompare());
  307. frq[m ] += frq[mm];
  308. push_heap (&fHeap[0], &fHeap[nf], FHeapCompare());
  309. //
  310. // The entries in scode are linked into lists with the
  311. // entries in hlink serving as "next" pointers and with
  312. // the end of a list marked by hlink[j] == j.
  313. //
  314. // Traverse the lists that start at scode[m] and scode[mm].
  315. // For each element visited, increment the length of the
  316. // corresponding code by one bit. (If we visit scode[j]
  317. // during the traversal, then the code for symbol j becomes
  318. // one bit longer.)
  319. //
  320. // Merge the lists that start at scode[m] and scode[mm]
  321. // into a single list that starts at scode[m].
  322. //
  323. //
  324. // Add a bit to all codes in the first list.
  325. //
  326. for (int j = m; true; j = hlink[j])
  327. {
  328. scode[j]++;
  329. assert (scode[j] <= 58);
  330. if (hlink[j] == j)
  331. {
  332. //
  333. // Merge the two lists.
  334. //
  335. hlink[j] = mm;
  336. break;
  337. }
  338. }
  339. //
  340. // Add a bit to all codes in the second list
  341. //
  342. for (int j = mm; true; j = hlink[j])
  343. {
  344. scode[j]++;
  345. assert (scode[j] <= 58);
  346. if (hlink[j] == j)
  347. break;
  348. }
  349. }
  350. //
  351. // Build a canonical Huffman code table, replacing the code
  352. // lengths in scode with (code, code length) pairs. Copy the
  353. // code table from scode into frq.
  354. //
  355. hufCanonicalCodeTable (scode);
  356. memcpy (frq, scode, sizeof (Int64) * HUF_ENCSIZE);
  357. }
  358. //
  359. // Pack an encoding table:
  360. // - only code lengths, not actual codes, are stored
  361. // - runs of zeroes are compressed as follows:
  362. //
  363. // unpacked packed
  364. // --------------------------------
  365. // 1 zero 0 (6 bits)
  366. // 2 zeroes 59
  367. // 3 zeroes 60
  368. // 4 zeroes 61
  369. // 5 zeroes 62
  370. // n zeroes (6 or more) 63 n-6 (6 + 8 bits)
  371. //
  372. const int SHORT_ZEROCODE_RUN = 59;
  373. const int LONG_ZEROCODE_RUN = 63;
  374. const int SHORTEST_LONG_RUN = 2 + LONG_ZEROCODE_RUN - SHORT_ZEROCODE_RUN;
  375. const int LONGEST_LONG_RUN = 255 + SHORTEST_LONG_RUN;
  376. void
  377. hufPackEncTable
  378. (const Int64* hcode, // i : encoding table [HUF_ENCSIZE]
  379. int im, // i : min hcode index
  380. int iM, // i : max hcode index
  381. char** pcode) // o: ptr to packed table (updated)
  382. {
  383. char *p = *pcode;
  384. Int64 c = 0;
  385. int lc = 0;
  386. for (; im <= iM; im++)
  387. {
  388. int l = hufLength (hcode[im]);
  389. if (l == 0)
  390. {
  391. int zerun = 1;
  392. while ((im < iM) && (zerun < LONGEST_LONG_RUN))
  393. {
  394. if (hufLength (hcode[im+1]) > 0 )
  395. break;
  396. im++;
  397. zerun++;
  398. }
  399. if (zerun >= 2)
  400. {
  401. if (zerun >= SHORTEST_LONG_RUN)
  402. {
  403. outputBits (6, LONG_ZEROCODE_RUN, c, lc, p);
  404. outputBits (8, zerun - SHORTEST_LONG_RUN, c, lc, p);
  405. }
  406. else
  407. {
  408. outputBits (6, SHORT_ZEROCODE_RUN + zerun - 2, c, lc, p);
  409. }
  410. continue;
  411. }
  412. }
  413. outputBits (6, l, c, lc, p);
  414. }
  415. if (lc > 0)
  416. *p++ = (unsigned char) (c << (8 - lc));
  417. *pcode = p;
  418. }
  419. //
  420. // Unpack an encoding table packed by hufPackEncTable():
  421. //
  422. void
  423. hufUnpackEncTable
  424. (const char** pcode, // io: ptr to packed table (updated)
  425. int ni, // i : input size (in bytes)
  426. int im, // i : min hcode index
  427. int iM, // i : max hcode index
  428. Int64* hcode) // o: encoding table [HUF_ENCSIZE]
  429. {
  430. memset (hcode, 0, sizeof (Int64) * HUF_ENCSIZE);
  431. const char *p = *pcode;
  432. Int64 c = 0;
  433. int lc = 0;
  434. for (; im <= iM; im++)
  435. {
  436. if (p - *pcode > ni)
  437. unexpectedEndOfTable();
  438. Int64 l = hcode[im] = getBits (6, c, lc, p); // code length
  439. if (l == (Int64) LONG_ZEROCODE_RUN)
  440. {
  441. if (p - *pcode > ni)
  442. unexpectedEndOfTable();
  443. int zerun = getBits (8, c, lc, p) + SHORTEST_LONG_RUN;
  444. if (im + zerun > iM + 1)
  445. tableTooLong();
  446. while (zerun--)
  447. hcode[im++] = 0;
  448. im--;
  449. }
  450. else if (l >= (Int64) SHORT_ZEROCODE_RUN)
  451. {
  452. int zerun = l - SHORT_ZEROCODE_RUN + 2;
  453. if (im + zerun > iM + 1)
  454. tableTooLong();
  455. while (zerun--)
  456. hcode[im++] = 0;
  457. im--;
  458. }
  459. }
  460. *pcode = const_cast<char *>(p);
  461. hufCanonicalCodeTable (hcode);
  462. }
  463. //
  464. // DECODING TABLE BUILDING
  465. //
  466. //
  467. // Clear a newly allocated decoding table so that it contains only zeroes.
  468. //
  469. void
  470. hufClearDecTable
  471. (HufDec * hdecod) // io: (allocated by caller)
  472. // decoding table [HUF_DECSIZE]
  473. {
  474. memset (hdecod, 0, sizeof (HufDec) * HUF_DECSIZE);
  475. }
  476. //
  477. // Build a decoding hash table based on the encoding table hcode:
  478. // - short codes (<= HUF_DECBITS) are resolved with a single table access;
  479. // - long code entry allocations are not optimized, because long codes are
  480. // unfrequent;
  481. // - decoding tables are used by hufDecode();
  482. //
  483. void
  484. hufBuildDecTable
  485. (const Int64* hcode, // i : encoding table
  486. int im, // i : min index in hcode
  487. int iM, // i : max index in hcode
  488. HufDec * hdecod) // o: (allocated by caller)
  489. // decoding table [HUF_DECSIZE]
  490. {
  491. //
  492. // Init hashtable & loop on all codes.
  493. // Assumes that hufClearDecTable(hdecod) has already been called.
  494. //
  495. for (; im <= iM; im++)
  496. {
  497. Int64 c = hufCode (hcode[im]);
  498. int l = hufLength (hcode[im]);
  499. if (c >> l)
  500. {
  501. //
  502. // Error: c is supposed to be an l-bit code,
  503. // but c contains a value that is greater
  504. // than the largest l-bit number.
  505. //
  506. invalidTableEntry();
  507. }
  508. if (l > HUF_DECBITS)
  509. {
  510. //
  511. // Long code: add a secondary entry
  512. //
  513. HufDec *pl = hdecod + (c >> (l - HUF_DECBITS));
  514. if (pl->len)
  515. {
  516. //
  517. // Error: a short code has already
  518. // been stored in table entry *pl.
  519. //
  520. invalidTableEntry();
  521. }
  522. pl->lit++;
  523. if (pl->p)
  524. {
  525. int *p = pl->p;
  526. pl->p = new int [pl->lit];
  527. for (int i = 0; i < pl->lit - 1; ++i)
  528. pl->p[i] = p[i];
  529. delete [] p;
  530. }
  531. else
  532. {
  533. pl->p = new int [1];
  534. }
  535. pl->p[pl->lit - 1]= im;
  536. }
  537. else if (l)
  538. {
  539. //
  540. // Short code: init all primary entries
  541. //
  542. HufDec *pl = hdecod + (c << (HUF_DECBITS - l));
  543. for (Int64 i = 1 << (HUF_DECBITS - l); i > 0; i--, pl++)
  544. {
  545. if (pl->len || pl->p)
  546. {
  547. //
  548. // Error: a short code or a long code has
  549. // already been stored in table entry *pl.
  550. //
  551. invalidTableEntry();
  552. }
  553. pl->len = l;
  554. pl->lit = im;
  555. }
  556. }
  557. }
  558. }
  559. //
  560. // Free the long code entries of a decoding table built by hufBuildDecTable()
  561. //
  562. void
  563. hufFreeDecTable (HufDec *hdecod) // io: Decoding table
  564. {
  565. for (int i = 0; i < HUF_DECSIZE; i++)
  566. {
  567. if (hdecod[i].p)
  568. {
  569. delete [] hdecod[i].p;
  570. hdecod[i].p = 0;
  571. }
  572. }
  573. }
  574. //
  575. // ENCODING
  576. //
  577. inline void
  578. outputCode (Int64 code, Int64 &c, int &lc, char *&out)
  579. {
  580. outputBits (hufLength (code), hufCode (code), c, lc, out);
  581. }
  582. inline void
  583. sendCode (Int64 sCode, int runCount, Int64 runCode,
  584. Int64 &c, int &lc, char *&out)
  585. {
  586. //
  587. // Output a run of runCount instances of the symbol sCount.
  588. // Output the symbols explicitly, or if that is shorter, output
  589. // the sCode symbol once followed by a runCode symbol and runCount
  590. // expressed as an 8-bit number.
  591. //
  592. if (hufLength (sCode) + hufLength (runCode) + 8 <
  593. hufLength (sCode) * runCount)
  594. {
  595. outputCode (sCode, c, lc, out);
  596. outputCode (runCode, c, lc, out);
  597. outputBits (8, runCount, c, lc, out);
  598. }
  599. else
  600. {
  601. while (runCount-- >= 0)
  602. outputCode (sCode, c, lc, out);
  603. }
  604. }
  605. //
  606. // Encode (compress) ni values based on the Huffman encoding table hcode:
  607. //
  608. int
  609. hufEncode // return: output size (in bits)
  610. (const Int64* hcode, // i : encoding table
  611. const unsigned short* in, // i : uncompressed input buffer
  612. const int ni, // i : input buffer size (in bytes)
  613. int rlc, // i : rl code
  614. char* out) // o: compressed output buffer
  615. {
  616. char *outStart = out;
  617. Int64 c = 0; // bits not yet written to out
  618. int lc = 0; // number of valid bits in c (LSB)
  619. int s = in[0];
  620. int cs = 0;
  621. //
  622. // Loop on input values
  623. //
  624. for (int i = 1; i < ni; i++)
  625. {
  626. //
  627. // Count same values or send code
  628. //
  629. if (s == in[i] && cs < 255)
  630. {
  631. cs++;
  632. }
  633. else
  634. {
  635. sendCode (hcode[s], cs, hcode[rlc], c, lc, out);
  636. cs=0;
  637. }
  638. s = in[i];
  639. }
  640. //
  641. // Send remaining code
  642. //
  643. sendCode (hcode[s], cs, hcode[rlc], c, lc, out);
  644. if (lc)
  645. *out = (c << (8 - lc)) & 0xff;
  646. return (out - outStart) * 8 + lc;
  647. }
  648. //
  649. // DECODING
  650. //
  651. //
  652. // In order to force the compiler to inline them,
  653. // getChar() and getCode() are implemented as macros
  654. // instead of "inline" functions.
  655. //
  656. #define getChar(c, lc, in) \
  657. { \
  658. c = (c << 8) | *(unsigned char *)(in++); \
  659. lc += 8; \
  660. }
  661. #define getCode(po, rlc, c, lc, in, out, ob, oe)\
  662. { \
  663. if (po == rlc) \
  664. { \
  665. if (lc < 8) \
  666. getChar(c, lc, in); \
  667. \
  668. lc -= 8; \
  669. \
  670. unsigned char cs = (c >> lc); \
  671. \
  672. if (out + cs > oe) \
  673. tooMuchData(); \
  674. else if (out - 1 < ob) \
  675. notEnoughData(); \
  676. \
  677. unsigned short s = out[-1]; \
  678. \
  679. while (cs-- > 0) \
  680. *out++ = s; \
  681. } \
  682. else if (out < oe) \
  683. { \
  684. *out++ = po; \
  685. } \
  686. else \
  687. { \
  688. tooMuchData(); \
  689. } \
  690. }
  691. //
  692. // Decode (uncompress) ni bits based on encoding & decoding tables:
  693. //
  694. void
  695. hufDecode
  696. (const Int64 * hcode, // i : encoding table
  697. const HufDec * hdecod, // i : decoding table
  698. const char* in, // i : compressed input buffer
  699. int ni, // i : input size (in bits)
  700. int rlc, // i : run-length code
  701. int no, // i : expected output size (in bytes)
  702. unsigned short* out) // o: uncompressed output buffer
  703. {
  704. Int64 c = 0;
  705. int lc = 0;
  706. unsigned short * outb = out;
  707. unsigned short * oe = out + no;
  708. const char * ie = in + (ni + 7) / 8; // input byte size
  709. //
  710. // Loop on input bytes
  711. //
  712. while (in < ie)
  713. {
  714. getChar (c, lc, in);
  715. //
  716. // Access decoding table
  717. //
  718. while (lc >= HUF_DECBITS)
  719. {
  720. const HufDec pl = hdecod[(c >> (lc-HUF_DECBITS)) & HUF_DECMASK];
  721. if (pl.len)
  722. {
  723. //
  724. // Get short code
  725. //
  726. lc -= pl.len;
  727. getCode (pl.lit, rlc, c, lc, in, out, outb, oe);
  728. }
  729. else
  730. {
  731. if (!pl.p)
  732. invalidCode(); // wrong code
  733. //
  734. // Search long code
  735. //
  736. int j;
  737. for (j = 0; j < pl.lit; j++)
  738. {
  739. int l = hufLength (hcode[pl.p[j]]);
  740. while (lc < l && in < ie) // get more bits
  741. getChar (c, lc, in);
  742. if (lc >= l)
  743. {
  744. if (hufCode (hcode[pl.p[j]]) ==
  745. ((c >> (lc - l)) & ((Int64(1) << l) - 1)))
  746. {
  747. //
  748. // Found : get long code
  749. //
  750. lc -= l;
  751. getCode (pl.p[j], rlc, c, lc, in, out, outb, oe);
  752. break;
  753. }
  754. }
  755. }
  756. if (j == pl.lit)
  757. invalidCode(); // Not found
  758. }
  759. }
  760. }
  761. //
  762. // Get remaining (short) codes
  763. //
  764. int i = (8 - ni) & 7;
  765. c >>= i;
  766. lc -= i;
  767. while (lc > 0)
  768. {
  769. const HufDec pl = hdecod[(c << (HUF_DECBITS - lc)) & HUF_DECMASK];
  770. if (pl.len)
  771. {
  772. lc -= pl.len;
  773. getCode (pl.lit, rlc, c, lc, in, out, outb, oe);
  774. }
  775. else
  776. {
  777. invalidCode(); // wrong (long) code
  778. }
  779. }
  780. if (out - outb != no)
  781. notEnoughData ();
  782. }
  783. void
  784. countFrequencies (Int64 freq[HUF_ENCSIZE],
  785. const unsigned short data[/*n*/],
  786. int n)
  787. {
  788. for (int i = 0; i < HUF_ENCSIZE; ++i)
  789. freq[i] = 0;
  790. for (int i = 0; i < n; ++i)
  791. ++freq[data[i]];
  792. }
  793. void
  794. writeUInt (char buf[4], unsigned int i)
  795. {
  796. unsigned char *b = (unsigned char *) buf;
  797. b[0] = i;
  798. b[1] = i >> 8;
  799. b[2] = i >> 16;
  800. b[3] = i >> 24;
  801. }
  802. unsigned int
  803. readUInt (const char buf[4])
  804. {
  805. const unsigned char *b = (const unsigned char *) buf;
  806. return ( b[0] & 0x000000ff) |
  807. ((b[1] << 8) & 0x0000ff00) |
  808. ((b[2] << 16) & 0x00ff0000) |
  809. ((b[3] << 24) & 0xff000000);
  810. }
  811. } // namespace
  812. //
  813. // EXTERNAL INTERFACE
  814. //
  815. int
  816. hufCompress (const unsigned short raw[],
  817. int nRaw,
  818. char compressed[])
  819. {
  820. if (nRaw == 0)
  821. return 0;
  822. AutoArray <Int64, HUF_ENCSIZE> freq;
  823. countFrequencies (freq, raw, nRaw);
  824. int im = 0;
  825. int iM = 0;
  826. hufBuildEncTable (freq, &im, &iM);
  827. char *tableStart = compressed + 20;
  828. char *tableEnd = tableStart;
  829. hufPackEncTable (freq, im, iM, &tableEnd);
  830. int tableLength = tableEnd - tableStart;
  831. char *dataStart = tableEnd;
  832. int nBits = hufEncode (freq, raw, nRaw, iM, dataStart);
  833. int dataLength = (nBits + 7) / 8;
  834. writeUInt (compressed, im);
  835. writeUInt (compressed + 4, iM);
  836. writeUInt (compressed + 8, tableLength);
  837. writeUInt (compressed + 12, nBits);
  838. writeUInt (compressed + 16, 0); // room for future extensions
  839. return dataStart + dataLength - compressed;
  840. }
  841. void
  842. hufUncompress (const char compressed[],
  843. int nCompressed,
  844. unsigned short raw[],
  845. int nRaw)
  846. {
  847. if (nCompressed == 0)
  848. {
  849. if (nRaw != 0)
  850. notEnoughData();
  851. return;
  852. }
  853. int im = readUInt (compressed);
  854. int iM = readUInt (compressed + 4);
  855. // int tableLength = readUInt (compressed + 8);
  856. int nBits = readUInt (compressed + 12);
  857. if (im < 0 || im >= HUF_ENCSIZE || iM < 0 || iM >= HUF_ENCSIZE)
  858. invalidTableSize();
  859. const char *ptr = compressed + 20;
  860. //
  861. // Fast decoder needs at least 2x64-bits of compressed data, and
  862. // needs to be run-able on this platform. Otherwise, fall back
  863. // to the original decoder
  864. //
  865. if (FastHufDecoder::enabled() && nBits > 128)
  866. {
  867. FastHufDecoder fhd (ptr, nCompressed - (ptr - compressed), im, iM, iM);
  868. fhd.decode ((unsigned char*)ptr, nBits, raw, nRaw);
  869. }
  870. else
  871. {
  872. AutoArray <Int64, HUF_ENCSIZE> freq;
  873. AutoArray <HufDec, HUF_DECSIZE> hdec;
  874. hufClearDecTable (hdec);
  875. hufUnpackEncTable (&ptr,
  876. nCompressed - (ptr - compressed),
  877. im,
  878. iM,
  879. freq);
  880. try
  881. {
  882. if (nBits > 8 * (nCompressed - (ptr - compressed)))
  883. invalidNBits();
  884. hufBuildDecTable (freq, im, iM, hdec);
  885. hufDecode (freq, hdec, ptr, nBits, iM, nRaw, raw);
  886. }
  887. catch (...)
  888. {
  889. hufFreeDecTable (hdec);
  890. throw;
  891. }
  892. hufFreeDecTable (hdec);
  893. }
  894. }
  895. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT