⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ebcot_arith_encoder.h

📁 JPEG2000 EBCOT算法源码
💻 H
字号:
/*****************************************************************************/
/* File name: "ebcot_arith_encoder.h"                                        */
/* Author: David Taubman                                                     */
/* Copyright 1998, Hewlett-Packard Company                                   */
/* All rights reserved                                                       */
/*****************************************************************************/
#ifndef EBCOT_ARITH_ENCODER_H
#define EBCOT_ARITH_ENCODER_H
#include <ebcot_common.h>
#include "ebcot_encoder.h"

/* ========================================================================= */
/* --------------------------- Utility Macros ------------------------------ */
/* ========================================================================= */

/*****************************************************************************/
/* MACRO                       check_overflow                                */
/*****************************************************************************/

#define check_overflow(_state_,_A_,_C_,_word_,_avail_,_rho_)              \
          /* arith_state_ptr _state_, std_int _A_, std_int _C_,  */       \
          /* std_int _word_, std_short _avail_, std_int _rho_    */       \
  {                                                                       \
    if (_C_ < 0)                                                          \
      { /* Carry out of interval lower bound register. */                 \
        _word_ <<= 1; _word_++; /* Push out a 1. */                       \
        if ((--_avail_) == 0)                                             \
          { _avail_ = 32;                                                 \
            ebcot_arith_coder__output_word(_state_,_word_); }             \
        _rho_--;                                                          \
        while (_rho_ > 0)                                                 \
          {                                                               \
            _word_ <<= 1; /* Push out a 0. */                             \
            if ((--_avail_) == 0)                                         \
              { _avail_ = 32;                                             \
                ebcot_arith_coder__output_word(_state_,_word_); }         \
            _rho_--;                                                      \
          }                                                               \
        _C_ &= ~(1<<31); /* Reset most significant bit. */                \
      }                                                                   \
  }

/*****************************************************************************/
/* MACRO                      renormalize_once                               */
/*****************************************************************************/

#define renormalize_once(_state_,_A_,_C_,_word_,_avail_,_rho_)        \
          /* arith_state_ptr _state_, std_int _A_, std_int _C_,   */  \
          /* std_int _word_, std_short _avail_, std_int _rho_     */  \
          /* Note: doesn't reset the most significant bit of _C_. */  \
  {                                                                   \
    _A_ <<= 1;                                                        \
    _C_ <<= 1;                                                        \
    if (_rho_ < 0)                                                    \
      {                                                               \
        if (_C_ < 0)                                                  \
          {                                                           \
            _word_ <<= 1; _word_++; /* Push out a 1. */               \
            if ((--_avail_) == 0)                                     \
              { _avail_ = 32;                                         \
                ebcot_arith_coder__output_word(_state_,_word_); }     \
          }                                                           \
        else                                                          \
          _rho_ = 0;                                                  \
      }                                                               \
    else                                                              \
      {                                                               \
        if (_C_ < 0)                                                  \
          _rho_++;                                                    \
        else                                                          \
          {                                                           \
            _word_ <<= 1; /* Push out a 0. */                         \
            if ((--_avail_) == 0)                                     \
              { _avail_ = 32;                                         \
                ebcot_arith_coder__output_word(_state_,_word_); }     \
            while (_rho_ > 0)                                         \
              {                                                       \
                _word_ <<= 1; _word_++; /* Push out a 1. */           \
                if ((--_avail_) == 0)                                 \
                  { _avail_ = 32;                                     \
                    ebcot_arith_coder__output_word(_state_,_word_); } \
                _rho_--;                                              \
              }                                                       \
          }                                                           \
      }                                                               \
  }


/* ========================================================================= */
/* ------------------------ Symbol Coding Macros --------------------------- */
/* ========================================================================= */

/*****************************************************************************/
/* MACRO                         emit_symbol                                 */
/*****************************************************************************/

#ifdef LOG_SYMBOLS
#  define emit_symbol(__state,__A,__C,__word,__avail,__rho,__symbol,__ctxt)  \
    {                                                                        \
      ebcot_arith_coder__log_symbol(__state,__ctxt,__symbol);                \
      _emit_symbol(__state,__A,__C,__word,__avail,__rho,__symbol,__ctxt);    \
    }
#else
#  define emit_symbol _emit_symbol
#endif /* LOG_SYMBOLS */

/*****************************************************************************/
/* MACRO                        _emit_symbol                                 */
/*****************************************************************************/

#define _emit_symbol(_state,_A,_C,_word,_avail,_rho,_symbol,_ctxt)        \
          /* arith_state_ptr _state, std_int _A, std_int _C,  */          \
          /* std_int _word, std_short _avail, std_int _rho,   */          \
          /* ifc_int _symbol, context_state_ptr _ctxt         */          \
  {                                                                       \
    context_state _cs;                                                    \
    std_int _pA;                                                          \
                                                                          \
    _increment_symbol_count                                               \
    _cs = *_ctxt;                                                         \
    _pA = ebcot_p0_lut[_cs];                                              \
    _pA *= _A;                                                            \
    if (_symbol == 0)                                                     \
      {                                                                   \
        _A = _pA;                                                         \
        _cs++; /* Increment total symbol count. */                        \
        if ((_cs & S_MASK) == 0)                                          \
          { /* Overflow detected. */                                      \
            _cs--;                                                        \
            _cs = (_cs>>1) & ~S_MSB;                                      \
            _cs++;                                                        \
          }                                                               \
      }                                                                   \
    else                                                                  \
      {                                                                   \
        _C += _pA;                                                        \
        _A = (_A<<P_BITS) - _pA;                                          \
        _cs += 1 + (1<<S_BITS); /* Increment S and N together. */         \
        if (((_cs & S_MASK) == 0) || (_cs & N_OVFL))                      \
          { /* Overflow detected. */                                      \
            _cs -= 1 + (1<<S_BITS);                                       \
            _cs = (_cs>>1) & ~S_MSB;                                      \
            _cs += 1 + (1<<S_BITS);                                       \
          }                                                               \
      }                                                                   \
    *_ctxt = _cs;                                                         \
    check_overflow(_state,_A,_C,_word,_avail,_rho);                       \
    if ((_A & (1<<30)) == 0)                                              \
      {                                                                   \
        do {                                                              \
          renormalize_once(_state,_A,_C,_word,_avail,_rho);               \
        } while ((_A & (1<<30)) == 0);                                    \
        _C &= ~(1<<31); /* Not done during renormalization. */            \
      }                                                                   \
    _A >>= P_BITS;                                                        \
  }

/*****************************************************************************/
/* MACRO                     emit_uniform_symbol                             */
/*****************************************************************************/

#ifdef LOG_SYMBOLS
#  define emit_uniform_symbol(__state,__A,__C,__word,__avail,__rho,__symbol) \
    {                                                                        \
      ebcot_arith_coder__log_symbol(__state,NULL,__symbol);                  \
      _emit_uniform_symbol(__state,__A,__C,__word,__avail,__rho,__symbol);   \
    }
#else
#  define emit_uniform_symbol _emit_uniform_symbol
#endif /* LOG_SYMBOLS */

/*****************************************************************************/
/* MACRO                    _emit_uniform_symbol                             */
/*****************************************************************************/

#define _emit_uniform_symbol(_state,_A,_C,_word,_avail,_rho,_symbol)    \
          /* arith_state_ptr _state, std_int _A, std_int _C,  */        \
          /* std_int _word, std_short _avail, std_int _rho,   */        \
          /* ifc_int _symbol                                  */        \
  {                                                                     \
    _increment_symbol_count                                             \
    _A <<= (P_BITS-1);                                                  \
    if (_symbol)                                                        \
      _C += _A;                                                         \
    check_overflow(_state,_A,_C,_word,_avail,_rho);                     \
    assert(((_A & (1<<30)) == 0) && (_A & (1<<29)));                    \
    renormalize_once(_state,_A,_C,_word,_avail,_rho);                   \
    _C &= ~(1<<31); /* Not done during renormalization. */              \
    _A >>= P_BITS;                                                      \
  }

/* ========================================================================= */
/* ------------------------- External Functions ---------------------------- */
/* ========================================================================= */

extern void
  ebcot_arith_coder__initialize(arith_state_ptr state, heap_unit_ptr heap,
                                int next_heap_pos, codeword_heap_ref code_mgr);

extern void
  ebcot_arith_coder__renormalize(arith_state_ptr state);

extern void
  ebcot_arith_coder__output_word(arith_state_ptr state, std_int word);

extern void
  ebcot_arith_coder__flush(arith_state_ptr state);

extern int
  ebcot_arith_coder__get_minimum_bits(arith_state_ptr state,
                                     heap_unit_ptr heap, int heap_pos);

#ifdef LOG_SYMBOLS
extern void
  ebcot_arith_coder__log_symbol(arith_state_ptr state, context_state_ptr ctxt,
                               std_short symbol);
#endif /* LOG_SYMBOLS */

#endif /* EBCOT_ARITH_ENCODER_H */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -