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

📄 block_decoder.cpp

📁 该源码是JPEG2000的c++源代码,希望对研究JPEG2000标准以及编解码的朋友们有用.
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*****************************************************************************/// File: block_decoder.cpp [scope = CORESYS/CODING]// Version: Kakadu, V2.2// Author: David Taubman// Last Revised: 20 June, 2001/*****************************************************************************/// Copyright 2001, David Taubman, The University of New South Wales (UNSW)// The copyright owner is Unisearch Ltd, Australia (commercial arm of UNSW)// Neither this copyright statement, nor the licensing details below// may be removed from this file or dissociated from its contents./*****************************************************************************/// Licensee: Book Owner// License number: 99999// The Licensee has been granted a NON-COMMERCIAL license to the contents of// this source file, said Licensee being the owner of a copy of the book,// "JPEG2000: Image Compression Fundamentals, Standards and Practice," by// Taubman and Marcellin (Kluwer Academic Publishers, 2001).  A brief summary// of the license appears below.  This summary is not to be relied upon in// preference to the full text of the license agreement, which was accepted// upon breaking the seal of the compact disc accompanying the above-mentioned// book.// 1. The Licensee has the right to Non-Commercial Use of the Kakadu software,//    Version 2.2, including distribution of one or more Applications built//    using the software, provided such distribution is not for financial//    return.// 2. The Licensee has the right to personal use of the Kakadu software,//    Version 2.2.// 3. The Licensee has the right to distribute Reusable Code (including//    source code and dynamically or statically linked libraries) to a Third//    Party, provided the Third Party possesses a license to use the Kakadu//    software, Version 2.2, and provided such distribution is not for//    financial return./******************************************************************************Description:   Implements the embedded block decoding algorithm, including the decodingpasses themselves, as well as error detection and concealment capabilities.No more than 30 bit-planes will be decoded for any code-block.  The lowlevel services offered by the MQ arithmetic coder appear in "mq_decoder.cpp"and "mq_decoder.h".******************************************************************************/#include <assert.h>#include <string.h>#include "kdu_messaging.h"#include "kdu_block_coding.h"#include "block_coding_common.h"#include "mq_decoder.h"static kdu_byte *significance_luts[4] =  {lh_sig_lut, hl_sig_lut, lh_sig_lut, hh_sig_lut};#define EXTRA_DECODE_CWORDS 3 // Number of extra context-words between stripes.// Set things up for the inclusion of assembler optimized routines for// specific architectures.  Optimizing compilers should generally do a// pretty good job with the C++ code contained here.  Further optimizations// are probably only worthwhile for processors with very few registers such// as the X86 family of processors.#ifdef KDU_PENTIUM_MSVC#  define KDU_ASM_OPTIMIZATIONS#  include "msvc_block_decode_asm.h"static int mmx_exists = msvc_decoder_mmx_exists();#endif/* ========================================================================= *//*                   Local Class and Structure Definitions                   *//* ========================================================================= *//*****************************************************************************//*                             kd_block_decoder                              *//*****************************************************************************/class kd_block_decoder : public kdu_block_decoder_base {  /* Although we can supply a constructor and a virtual destructor in the     future, we have no need for these for the moment. */  protected:    void decode(kdu_block *block);  private: // Internal implementation    void reset_states()      { // See Table 12.1 in the book by Taubman and Marcellin        for (int n=0; n < 18; n++)          states[n].init(0,0);        states[KAPPA_SIG_BASE].init(4,0);        states[KAPPA_RUN_BASE].init(3,0);      }  private: // Data    mq_decoder coder;    mqd_state states[18];  };/* ========================================================================= *//*             Binding of MQ and Raw Symbol Coding Services                  *//* ========================================================================= */#define USE_FAST_MACROS // Comment this out if you want functions instead.#ifdef USE_FAST_MACROS#  define _mq_check_out_(coder)                                     \     register kdu_int32 D; register kdu_int32 C; register kdu_int32 A; \     register kdu_int32 t; kdu_int32 temp; kdu_byte *store; int S;     \     coder.check_out(A,C,D,t,temp,store,S)#  define _mq_check_in_(coder)                                      \     coder.check_in(A,C,D,t,temp,store,S)#  define _mq_dec_(coder,symbol,state)                              \     _mq_decode_(symbol,state,A,C,D,t,temp,store,S)#  define _mq_dec_run_(coder,run)                                   \     _mq_decode_run_(run,A,C,D,t,temp,store,S)#  define _raw_check_out_(coder)                                    \     register kdu_int32 t; register kdu_int32 temp; kdu_byte *store;   \     coder.check_out(t,temp,store)#  define _raw_check_in_(coder)                                     \     coder.check_in(t,temp,store)#  define _raw_dec_(coder,symbol)                                   \     _raw_decode_(symbol,t,temp,store)#else // Do not use fast macros#  define _mq_check_out_(coder)#  define _mq_check_in_(coder)#  define _mq_dec_(coder,symbol,state) coder.mq_decode(symbol,state)#  define _mq_dec_run_(coder,run) coder.mq_decode_run(run)#  define _raw_check_out_(coder)#  define _raw_check_in_(coder)#  define _raw_dec_(coder,symbol) coder.raw_decode(symbol)#endif // USE_FAST_MACROS/* ========================================================================= *//*                 Machine Independent coding pass functions                 *//* ========================================================================= *//*****************************************************************************//* STATIC                   decode_sig_prop_pass_raw                         *//*****************************************************************************/static void  decode_sig_prop_pass_raw(mq_decoder &coder, int p, bool causal,                           kdu_int32 *samples, kdu_int32 *contexts,                           int width, int num_stripes, int context_row_gap){  /* Ideally, register storage is available for 7 32-bit integers. Two     are declared inside the "_raw_check_out_" macro.  The order of priority     for these registers corresponds roughly to the order in which their     declarations appear below.  Unfortunately, none of these register     requests are likely to be honored by the register-starved X86 family     of processors, but the register declarations may prove useful to     compilers for other architectures or for hand optimizations of     assembly code. */  register kdu_int32 *cp = contexts;  register int c;  register kdu_int32 cword;  _raw_check_out_(coder); // Declares t and temp as registers.  register kdu_int32 *sp = samples;  register kdu_int32 sym;  kdu_int32 one_point_five = 1<<p; one_point_five += (one_point_five>>1);  int r, width_by2=width+width, width_by3=width_by2+width;  assert((context_row_gap - width) == EXTRA_DECODE_CWORDS);  for (r=num_stripes; r > 0; r--, cp += EXTRA_DECODE_CWORDS, sp += width_by3)    for (c=width; c > 0; c--, sp++, cp++)      {        if (*cp == 0)          continue;        cword = *cp;        if ((cword & (NBRHD_MASK<<0)) && !(cword & (SIG_PROP_MEMBER_MASK<<0)))          { // Process first row of stripe column (row 0)            _raw_dec_(coder,sym);            if (!sym)              { cword |= (PI_BIT<<0); goto row_1; }            // Decode sign bit            _raw_dec_(coder,sym);            // Broadcast neighbourhood context changes            if (!causal)              {                cp[-context_row_gap-1] |=(SIGMA_BR_BIT<<9);                cp[-context_row_gap  ] |=(SIGMA_BC_BIT<<9)|(sym<<NEXT_CHI_POS);                cp[-context_row_gap+1] |=(SIGMA_BL_BIT<<9);              }            cp[-1] |= (SIGMA_CR_BIT<<0);            cp[1]  |= (SIGMA_CL_BIT<<0);            cword |= (SIGMA_CC_BIT<<0) | (PI_BIT<<0) | (sym<<CHI_POS);            sp[0] = (sym<<31) + one_point_five;          }row_1:        if ((cword & (NBRHD_MASK<<3)) && !(cword & (SIG_PROP_MEMBER_MASK<<3)))          { // Process second row of stripe column (row 1)            _raw_dec_(coder,sym);            if (!sym)              { cword |= (PI_BIT<<3); goto row_2; }            // Decode sign bit            _raw_dec_(coder,sym);            // Broadcast neighbourhood context changes            cp[-1] |= (SIGMA_CR_BIT<<3);            cp[1]  |= (SIGMA_CL_BIT<<3);            cword |= (SIGMA_CC_BIT<<3) | (PI_BIT<<3) | (sym<<(CHI_POS+3));            sp[width] = (sym<<31) + one_point_five;          }row_2:        if ((cword & (NBRHD_MASK<<6)) && !(cword & (SIG_PROP_MEMBER_MASK<<6)))          { // Process third row of stripe column (row 2)            _raw_dec_(coder,sym);            if (!sym)              { cword |= (PI_BIT<<6); goto row_3; }            // Decode sign bit            _raw_dec_(coder,sym);            // Broadcast neighbourhood context changes            cp[-1] |= (SIGMA_CR_BIT<<6);            cp[1]  |= (SIGMA_CL_BIT<<6);            cword |= (SIGMA_CC_BIT<<6) | (PI_BIT<<6) | (sym << (CHI_POS+6));            sp[width_by2] = (sym<<31) + one_point_five;          }row_3:        if ((cword & (NBRHD_MASK<<9)) && !(cword & (SIG_PROP_MEMBER_MASK<<9)))          { // Process fourth row of stripe column (row 3)            _raw_dec_(coder,sym);            if (!sym)              { cword |= (PI_BIT<<9); goto done; }            // Decode sign bit            _raw_dec_(coder,sym);            // Broadcast neighbourhood context changes            cp[context_row_gap-1] |= SIGMA_TR_BIT;            cp[context_row_gap  ] |= SIGMA_TC_BIT | (sym<<PREV_CHI_POS);            cp[context_row_gap+1] |= SIGMA_TL_BIT;            cp[-1] |= (SIGMA_CR_BIT<<9);            cp[1]  |= (SIGMA_CL_BIT<<9);            cword |= (SIGMA_CC_BIT<<9) | (PI_BIT<<9) | (sym<<(CHI_POS+9));            sp[width_by3] = (sym<<31) + one_point_five;          }done:        *cp = cword;      }  _raw_check_in_(coder);}/*****************************************************************************//* STATIC                    decode_sig_prop_pass                            *//*****************************************************************************/static void  decode_sig_prop_pass(mq_decoder &coder, mqd_state states[],                       int p, bool causal, int orientation,                       kdu_int32 *samples, kdu_int32 *contexts,                       int width, int num_stripes, int context_row_gap){  /* Ideally, register storage is available for 12 32-bit integers. Four     are declared inside the "_mq_check_out_" macro.  The order of priority     for these registers corresponds roughly to the order in which their     declarations appear below.  Unfortunately, none of these register     requests are likely to be honored by the register-starved X86 family     of processors, but the register declarations may prove useful to     compilers for other architectures or for hand optimizations of     assembly code. */  register kdu_int32 *cp = contexts;  register int c;  register kdu_int32 cword;  _mq_check_out_(coder); // Declares A, C, D and t as registers.  register kdu_int32 sym;  register kdu_int32 val;  register  kdu_byte *sig_lut = significance_luts[orientation];  register kdu_int32 *sp = samples;  register mqd_state *state_ref;  kdu_int32 one_point_five = 1<<p; one_point_five += (one_point_five>>1);  int r, width_by2=width+width, width_by3=width_by2+width;  assert((context_row_gap - width) == EXTRA_DECODE_CWORDS);  for (r=num_stripes; r > 0; r--, cp += EXTRA_DECODE_CWORDS, sp += width_by3)    for (c=width; c > 0; c--, sp++, cp++)      {        if (*cp == 0)          { // Invoke speedup trick to skip over runs of all-0 neighbourhoods            for (cp+=3; *cp == 0; cp+=3, c-=3, sp+=3);            cp-=3;            continue;          }        cword = *cp;        if ((cword & (NBRHD_MASK<<0)) && !(cword & (SIG_PROP_MEMBER_MASK<<0)))          { // Process first row of stripe column (row 0)            state_ref = states+KAPPA_SIG_BASE+sig_lut[cword & NBRHD_MASK];            _mq_dec_(coder,sym,*state_ref);            if (!sym)              { cword |= (PI_BIT<<0); goto row_1; }            // Decode sign bit            sym = cword & ((CHI_BIT>>3) | (SIGMA_CC_BIT>>3) |                           (CHI_BIT<<3) | (SIGMA_CC_BIT<<3));            sym >>= 1; // Shift down so that top sigma bit has address 0            sym |= (cp[-1] & ((CHI_BIT<<0) | (SIGMA_CC_BIT<<0))) >> (1+1);            sym |= (cp[ 1] & ((CHI_BIT<<0) | (SIGMA_CC_BIT<<0))) >> (1-1);            sym |= (sym >> (CHI_POS-1-SIGMA_CC_POS)); // Interleave chi & sigma            val = sign_lut[sym & 0x000000FF];            state_ref = states + KAPPA_SIGN_BASE + (val>>1);            _mq_dec_(coder,sym,*state_ref);            sym ^= (val & 1); // Sign bit recovered in LSB.            // Broadcast neighbourhood context changes            if (!causal)              {                cp[-context_row_gap-1] |=(SIGMA_BR_BIT<<9);                cp[-context_row_gap  ] |=(SIGMA_BC_BIT<<9)|(sym<<NEXT_CHI_POS);                cp[-context_row_gap+1] |=(SIGMA_BL_BIT<<9);              }            cp[-1] |= (SIGMA_CR_BIT<<0);            cp[1]  |= (SIGMA_CL_BIT<<0);            cword |= (SIGMA_CC_BIT<<0) | (PI_BIT<<0) | (sym<<CHI_POS);            sp[0] = (sym<<31) + one_point_five;          }row_1:        if ((cword & (NBRHD_MASK<<3)) && !(cword & (SIG_PROP_MEMBER_MASK<<3)))          { // Process second row of stripe column (row 1)            state_ref = states+KAPPA_SIG_BASE+sig_lut[(cword>>3) & NBRHD_MASK];            _mq_dec_(coder,sym,*state_ref);            if (!sym)              { cword |= (PI_BIT<<3); goto row_2; }            // Decode sign bit            sym = cword & ((CHI_BIT<<0) | (SIGMA_CC_BIT<<0) |                           (CHI_BIT<<6) | (SIGMA_CC_BIT<<6));            sym >>= 4; // Shift down so that top sigma bit has address 0            sym |= (cp[-1] & ((CHI_BIT<<3) | (SIGMA_CC_BIT<<3))) >> (4+1);            sym |= (cp[ 1] & ((CHI_BIT<<3) | (SIGMA_CC_BIT<<3))) >> (4-1);            sym |= (sym >> (CHI_POS-1-SIGMA_CC_POS)); // Interleave chi & sigma            val = sign_lut[sym & 0x000000FF];            state_ref = states + KAPPA_SIGN_BASE + (val>>1);            _mq_dec_(coder,sym,*state_ref);

⌨️ 快捷键说明

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