Blame IlmImf/ImfFastHuf.h

Packit Service 6754ca
///////////////////////////////////////////////////////////////////////////
Packit Service 6754ca
//
Packit Service 6754ca
// Copyright (c) 2009-2014 DreamWorks Animation LLC. 
Packit Service 6754ca
//
Packit Service 6754ca
// All rights reserved.
Packit Service 6754ca
//
Packit Service 6754ca
// Redistribution and use in source and binary forms, with or without
Packit Service 6754ca
// modification, are permitted provided that the following conditions are
Packit Service 6754ca
// met:
Packit Service 6754ca
// *       Redistributions of source code must retain the above copyright
Packit Service 6754ca
// notice, this list of conditions and the following disclaimer.
Packit Service 6754ca
// *       Redistributions in binary form must reproduce the above
Packit Service 6754ca
// copyright notice, this list of conditions and the following disclaimer
Packit Service 6754ca
// in the documentation and/or other materials provided with the
Packit Service 6754ca
// distribution.
Packit Service 6754ca
// *       Neither the name of DreamWorks Animation nor the names of
Packit Service 6754ca
// its contributors may be used to endorse or promote products derived
Packit Service 6754ca
// from this software without specific prior written permission.
Packit Service 6754ca
//
Packit Service 6754ca
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit Service 6754ca
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit Service 6754ca
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit Service 6754ca
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit Service 6754ca
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit Service 6754ca
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit Service 6754ca
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 6754ca
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 6754ca
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 6754ca
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit Service 6754ca
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 6754ca
//
Packit Service 6754ca
///////////////////////////////////////////////////////////////////////////
Packit Service 6754ca
Packit Service 6754ca
#ifndef INCLUDED_IMF_FAST_HUF_H
Packit Service 6754ca
#define INCLUDED_IMF_FAST_HUF_H
Packit Service 6754ca
Packit Service 6754ca
#include "ImfInt64.h"
Packit Service 6754ca
#include "ImfNamespace.h"
Packit Service 6754ca
Packit Service 6754ca
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
Packit Service 6754ca
Packit Service 6754ca
//
Packit Service 6754ca
// Alternative Canonical Huffman decoder:
Packit Service 6754ca
//
Packit Service 6754ca
// Canonical Huffman decoder based on 'On the Implementation of Minimum
Packit Service 6754ca
// Redundancy Prefix Codes' by Moffat and Turpin - highly recommended
Packit Service 6754ca
// reading as a good description of the problem space, as well as 
Packit Service 6754ca
// a fast decoding algorithm.
Packit Service 6754ca
//
Packit Service 6754ca
// The premise is that instead of working directly with the coded 
Packit Service 6754ca
// symbols, we create a new ordering based on the frequency of symbols.
Packit Service 6754ca
// Less frequent symbols (and thus longer codes) are ordered earler.
Packit Service 6754ca
// We're calling the values in this ordering 'Ids', as oppsed to 
Packit Service 6754ca
// 'Symbols' - which are the short values we eventually want decoded.
Packit Service 6754ca
//
Packit Service 6754ca
// With this new ordering, a few small tables can be derived ('base' 
Packit Service 6754ca
// and 'offset') which drive the decoding. To cut down on the 
Packit Service 6754ca
// linear scanning of these tables, you can add a small table
Packit Service 6754ca
// to directly look up short codes (as you might in a traditional
Packit Service 6754ca
// lookup-table driven decoder). 
Packit Service 6754ca
//
Packit Service 6754ca
// The decoder is meant to be compatible with the encoder (and decoder)
Packit Service 6754ca
// in ImfHuf.cpp, just faster. For ease of implementation, this decoder
Packit Service 6754ca
// should only be used on compressed bitstreams >= 128 bits long.
Packit Service 6754ca
//
Packit Service 6754ca
Packit Service 6754ca
class FastHufDecoder
Packit Service 6754ca
{
Packit Service 6754ca
  public:
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Longest compressed code length that ImfHuf supports (58 bits)
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    static const int MAX_CODE_LEN = 58;
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // Number of bits in our acceleration table. Should match all
Packit Service 6754ca
    // codes up to TABLE_LOOKUP_BITS in length.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    static const int TABLE_LOOKUP_BITS = 12;
Packit Service 6754ca
Packit Service 6754ca
    FastHufDecoder (const char*& table,
Packit Service 6754ca
                    int numBytes,
Packit Service 6754ca
                    int minSymbol,
Packit Service 6754ca
                    int maxSymbol,
Packit Service 6754ca
                    int rleSymbol);
Packit Service 6754ca
Packit Service 6754ca
    ~FastHufDecoder ();
Packit Service 6754ca
Packit Service 6754ca
    static bool enabled ();
Packit Service 6754ca
Packit Service 6754ca
    void decode (const unsigned char *src,
Packit Service 6754ca
                 int numSrcBits,
Packit Service 6754ca
                 unsigned short *dst,
Packit Service 6754ca
                 int numDstElems);
Packit Service 6754ca
Packit Service 6754ca
  private:
Packit Service 6754ca
Packit Service 6754ca
    void  buildTables (Int64*, Int64*);
Packit Service 6754ca
    void  refill (Int64&, int, Int64&, int&, const unsigned char *&, int&);
Packit Service 6754ca
    Int64 readBits (int, Int64&, int&, const char *&);
Packit Service 6754ca
Packit Service 6754ca
    int             _rleSymbol;        // RLE symbol written by the encoder.
Packit Service 6754ca
                                       // This could be 65536, so beware
Packit Service 6754ca
                                       // when you use shorts to hold things.
Packit Service 6754ca
Packit Service 6754ca
    int             _numSymbols;       // Number of symbols in the codebook.
Packit Service 6754ca
Packit Service 6754ca
    unsigned char   _minCodeLength;    // Minimum code length, in bits.
Packit Service 6754ca
    unsigned char   _maxCodeLength;    // Maximum code length, in bits.
Packit Service 6754ca
Packit Service 6754ca
    int            *_idToSymbol;       // Maps Ids to symbols. Ids are a symbol
Packit Service 6754ca
                                       // ordering sorted first in terms of 
Packit Service 6754ca
                                       // code length, and by code within
Packit Service 6754ca
                                       // the same length. Ids run from 0
Packit Service 6754ca
                                       // to mNumSymbols-1.
Packit Service 6754ca
Packit Service 6754ca
    Int64 _ljBase[MAX_CODE_LEN + 1];   // the 'left justified base' table.
Packit Service 6754ca
                                       // Takes base[i] (i = code length)
Packit Service 6754ca
                                       // and 'left justifies' it into an Int64
Packit Service 6754ca
Packit Service 6754ca
    Int64 _ljOffset[MAX_CODE_LEN +1 ]; // There are some other terms that can 
Packit Service 6754ca
                                       // be folded into constants when taking
Packit Service 6754ca
                                       // the 'left justified' decode path. This
Packit Service 6754ca
                                       // holds those constants, indexed by
Packit Service 6754ca
                                       // code length
Packit Service 6754ca
Packit Service 6754ca
    //
Packit Service 6754ca
    // We can accelerate the 'left justified' processing by running the
Packit Service 6754ca
    // top TABLE_LOOKUP_BITS through a LUT, to find the symbol and code
Packit Service 6754ca
    // length. These are those acceleration tables.
Packit Service 6754ca
    //
Packit Service 6754ca
    // Even though our evental 'symbols' are ushort's, the encoder adds
Packit Service 6754ca
    // a symbol to indicate RLE. So with a dense code book, we could
Packit Service 6754ca
    // have 2^16+1 codes, so both mIdToSymbol and mTableSymbol need
Packit Service 6754ca
    // to be bigger than 16 bits.
Packit Service 6754ca
    //
Packit Service 6754ca
Packit Service 6754ca
    int            _tableSymbol[1 << TABLE_LOOKUP_BITS];
Packit Service 6754ca
    unsigned char  _tableCodeLen[1 << TABLE_LOOKUP_BITS];
Packit Service 6754ca
    Int64          _tableMin;
Packit Service 6754ca
};
Packit Service 6754ca
Packit Service 6754ca
OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
Packit Service 6754ca
Packit Service 6754ca
#endif