Blame IlmImf/ImfFastHuf.h

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