ImfFastHuf.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #ifndef INCLUDED_IMF_FAST_HUF_H
  34. #define INCLUDED_IMF_FAST_HUF_H
  35. #include "ImfInt64.h"
  36. #include "ImfNamespace.h"
  37. #include "ImfExport.h"
  38. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  39. //
  40. // Alternative Canonical Huffman decoder:
  41. //
  42. // Canonical Huffman decoder based on 'On the Implementation of Minimum
  43. // Redundancy Prefix Codes' by Moffat and Turpin - highly recommended
  44. // reading as a good description of the problem space, as well as
  45. // a fast decoding algorithm.
  46. //
  47. // The premise is that instead of working directly with the coded
  48. // symbols, we create a new ordering based on the frequency of symbols.
  49. // Less frequent symbols (and thus longer codes) are ordered earler.
  50. // We're calling the values in this ordering 'Ids', as oppsed to
  51. // 'Symbols' - which are the short values we eventually want decoded.
  52. //
  53. // With this new ordering, a few small tables can be derived ('base'
  54. // and 'offset') which drive the decoding. To cut down on the
  55. // linear scanning of these tables, you can add a small table
  56. // to directly look up short codes (as you might in a traditional
  57. // lookup-table driven decoder).
  58. //
  59. // The decoder is meant to be compatible with the encoder (and decoder)
  60. // in ImfHuf.cpp, just faster. For ease of implementation, this decoder
  61. // should only be used on compressed bitstreams >= 128 bits long.
  62. //
  63. class FastHufDecoder
  64. {
  65. public:
  66. //
  67. // Longest compressed code length that ImfHuf supports (58 bits)
  68. //
  69. static const int MAX_CODE_LEN = 58;
  70. //
  71. // Number of bits in our acceleration table. Should match all
  72. // codes up to TABLE_LOOKUP_BITS in length.
  73. //
  74. static const int TABLE_LOOKUP_BITS = 12;
  75. IMF_EXPORT
  76. FastHufDecoder (const char*& table,
  77. int numBytes,
  78. int minSymbol,
  79. int maxSymbol,
  80. int rleSymbol);
  81. IMF_EXPORT
  82. ~FastHufDecoder ();
  83. IMF_EXPORT
  84. static bool enabled ();
  85. IMF_EXPORT
  86. void decode (const unsigned char *src,
  87. int numSrcBits,
  88. unsigned short *dst,
  89. int numDstElems);
  90. private:
  91. void buildTables (Int64*, Int64*);
  92. void refill (Int64&, int, Int64&, int&, const unsigned char *&, int&);
  93. Int64 readBits (int, Int64&, int&, const char *&);
  94. int _rleSymbol; // RLE symbol written by the encoder.
  95. // This could be 65536, so beware
  96. // when you use shorts to hold things.
  97. int _numSymbols; // Number of symbols in the codebook.
  98. unsigned char _minCodeLength; // Minimum code length, in bits.
  99. unsigned char _maxCodeLength; // Maximum code length, in bits.
  100. int *_idToSymbol; // Maps Ids to symbols. Ids are a symbol
  101. // ordering sorted first in terms of
  102. // code length, and by code within
  103. // the same length. Ids run from 0
  104. // to mNumSymbols-1.
  105. Int64 _ljBase[MAX_CODE_LEN + 1]; // the 'left justified base' table.
  106. // Takes base[i] (i = code length)
  107. // and 'left justifies' it into an Int64
  108. Int64 _ljOffset[MAX_CODE_LEN +1 ]; // There are some other terms that can
  109. // be folded into constants when taking
  110. // the 'left justified' decode path. This
  111. // holds those constants, indexed by
  112. // code length
  113. //
  114. // We can accelerate the 'left justified' processing by running the
  115. // top TABLE_LOOKUP_BITS through a LUT, to find the symbol and code
  116. // length. These are those acceleration tables.
  117. //
  118. // Even though our evental 'symbols' are ushort's, the encoder adds
  119. // a symbol to indicate RLE. So with a dense code book, we could
  120. // have 2^16+1 codes, so both mIdToSymbol and mTableSymbol need
  121. // to be bigger than 16 bits.
  122. //
  123. int _tableSymbol[1 << TABLE_LOOKUP_BITS];
  124. unsigned char _tableCodeLen[1 << TABLE_LOOKUP_BITS];
  125. Int64 _tableMin;
  126. };
  127. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  128. #endif