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

📄 ebcot_decoder.c

📁 JPEG2000实现的源码
💻 C
📖 第 1 页 / 共 5 页
字号:
/*****************************************************************************/
/* Copyright 1998, Hewlett-Packard Company                                   */
/* All rights reserved                                                       */
/* File: "ebcot_decoder.h"                                                   */
/* Description: Central source file for EBCOT decoder                        */
/* Author: David Taubman                                                     */
/* Affiliation: Hewlett-Packard and                                          */
/*              The University of New South Wales, Australia                 */
/* Version: VM9.0                                                            */
/* Last Revised: 18 April, 2001                                              */
/*****************************************************************************/

/*****************************************************************************/
/* Modified to incorporate MQ-coder by Mitsubishi Electric Corp.             */
/* Copyright 1999, Mitsubishi Electric Corp.                                 */
/* All rights reserved for modified parts                                    */
/*****************************************************************************/

/*****************************************************************************/
/* Modified to incorporate optional entropy coder                            */
/* Copyright 1999 Science Applications International Corporation (SAIC).     */
/* All Rights Reserved for modified parts.                                   */
/*****************************************************************************/

/*****************************************************************************/
/* Modifications tagged with comment lines or delimiters involving the       */
/* string, "David T mod" or "mod by David T", have been made by David        */
/* Taubman; they are copyrighted by Hewlett-Packard Company with all rights  */
/* reserved for the modified parts.                                          */
/*****************************************************************************/

/*****************************************************************************/
/* Modified by David Taubman to support new termination methods for parallel */
/* coding options, to enable more efficient implementations and to support   */
/* error resilience.  Copyright 1999 by Hewlett-Packard Company with all     */
/* rights reserved for the modified parts.                                   */
/*****************************************************************************/

/*****************************************************************************/
/* Modified to combine entropy coders                                        */
/* Copyright 1999 Science Applications International Corporation (SAIC).     */
/* Copyright 1999 University of Arizona, Arizona Board of Regents.           */
/* All Rights Reserved for modified parts.                                   */
/*****************************************************************************/

/*****************************************************************************/
/* Modified to include TCQ                                                   */
/* Copyright 1999 Science Applications International Corporation (SAIC).     */
/* Copyright 1995 University of Arizona, Arizona Board of Regents.           */
/* All Rights Reserved for modified parts.                                   */
/*****************************************************************************/

/*****************************************************************************/
/* Modified by David Taubman to support arbitrary reference points for the   */
/* transform and the various regular partitions, so as to facilitate         */
/* cropping and geometric transformations in the compressed domain and to    */
/* enable full support for CRF's single-sample overlap Wavelet transform,    */
/* as originally documented in Seoul.  Changes are too numerous to flag      */
/* individually within the code.  Changes copyrighted by HP with all rights  */
/* reserved for the modified parts.                                          */
/*****************************************************************************/

/*****************************************************************************/
/* Modified by David Taubman to support interface modifications, arbitrary   */
/* changes in coding parameters from component to component and from tile    */
/* to tile, packet partitions, rich packet sequencing conventions and to     */
/* support the full generality of PART-1 of the JPEG2000                     */
/* standard, and to support most anticipated generality of PART-2.  Changes  */
/* are too numerous to flag individually within the code, which in some      */
/* places has been completely rewritten.  All changes copyrighted by HP with */
/* all rights reserved for the modified parts.                               */
/*****************************************************************************/

/*****************************************************************************/
/* Modified by David Taubman to implement changes in Tokyo between CD and    */
/* FCD for Part-1 of the standard.  Copyrighted by HP with all rights        */
/* reserved for the modified parts.                                          */
/*****************************************************************************/

/*****************************************************************************/
/* Modified for general wavelet decompositions.                              */
/* Copyright 2000 Science Applications International Corporation (SAIC).     */
/* Copyright 1995 University of Arizona, Arizona Board of Regents.           */
/* All Rights Reserved for modified parts.                                   */
/*****************************************************************************/

/*****************************************************************************/
/* Modified for general offset and scalar quantization.                      */
/* Copyright 2000 The MITRE Corporation.                                     */
/* All Rights Reserved for modified parts.                                   */
/*****************************************************************************/

/*****************************************************************************/
/* Modified by David Taubman to incorporate changes to the POC marker (used  */
/* to be POD).  Changes are quite extensive and not always explicitly        */
/* When flagged, the relevant comment string is "DST POC mod".               */
/* Copyright 2000 The University of New South Wales.                         */
/* All rights reserved for the modified parts.                               */
/*****************************************************************************/

/*****************************************************************************/
/* Modified by David Taubman to correct behaviour when resolution levels     */
/* have zero area in some tile-component.  Changes flagged by the comment    */
/* string, "DST non-existent precinct fix".                                  */
/* Copyright 2001 The University of New South Wales.                         */
/* All rights reserved for the modified parts.                               */
/*****************************************************************************/


#include <local_services.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <time.h>
#include <ifc.h>
#include <markers.h>
#include "ebcot_common.h"
#include "ebcot_decoder.h"
#include "ebcot_receive_bits.h"

static int
  retrieve_next_packet(ebcot_decoder_ref self);


/* ========================================================================= */
/* ----------------------------- Utility Functions ------------------------- */
/* ========================================================================= */

/*****************************************************************************/
/* STATIC                          is_power_of_2                             */
/*****************************************************************************/

static int
  is_power_of_2(int val)
{
  if (val == 0)
    return(0);
  while (!(val & 1))
    val>>=1;
  return(val == 1);
}

/*****************************************************************************/
/* STATIC                       start_symbol_log                             */
/*****************************************************************************/
#ifdef DST_LOG_SYMBOLS
static FILE *
  start_symbol_log(ebcot_band_info_ptr band, ebcot_block_info_ptr block)
{
  FILE *fp;
  char fname[81];

  if (band->tnum != 0)
    local_error("Symbol logging is permitted only on single tile images!");
  sprintf(fname,"s_%d_%d_%d",band->level_idx,band->band_idx,
          (int)(block-band->blocks));
  fp = fopen(fname,"rb");
  assert(fp != NULL);
  return(fp);
}
#endif /* DST_LOG_SYMBOLS */

/* SAIC General Decomp. Begin mods */
/*****************************************************************************/
/* STATIC              convert_dimension_from_level_to_band                  */
/*****************************************************************************/

static int
  convert_dimension_from_level_to_band(int dim, int is_vertical,
                                       int max_hp_descent, int hp_descent, 
                                       int *hp_descent_chg,
                                       int band_idx)
{
  int d, orient, v;
  int prev_hp_descent;
  int dn, d_idx;

  v = (is_vertical)?1:0;
  prev_hp_descent = max_hp_descent;
  for (dn=max_hp_descent-hp_descent_chg[0], d_idx = 0;
       dn >= max_hp_descent-hp_descent;
       dn-=hp_descent_chg[d_idx+1], d_idx++)
    {
      assert(band_idx != LL_BAND);
      assert(d_idx < 3);
      d = max_hp_descent - d_idx - 1;
      orient = (band_idx >> (d+d)) & 3;
      switch (orient) {
        case LL_BAND:
          if (dn < prev_hp_descent)
            dim = (dim+1)>>1; break;
        case HL_BAND:
          if (dn < prev_hp_descent)
            dim = (dim+v)>>1; break;
        case LH_BAND:
          if (dn < prev_hp_descent)
            dim = (dim+1-v)>>1; break;
        case HH_BAND:
          if (dn < prev_hp_descent)
            dim = dim >> 1; break;
        default: assert(0);
      }
      prev_hp_descent = dn;
    }
  return(dim);
}
/* SAIC General Decomp. End mods */

/* ========================================================================= */
/* --------------------------- Decoding Functions -------------------------- */
/* ========================================================================= */

/*****************************************************************************/
/* STATIC                   undo_roi_boost_using_mask                        */
/*****************************************************************************/

static void
  undo_roi_boost_using_mask(ifc_int *sample_buffer, int rows, int cols,
                            int row_gap, int boost_delta,
                            std_byte **boost_mask)

 /* This function is called after decoding a block for which the ROI boost
    values differ for different samples in the block in a manner which
    cannot be deduced implicitly simply by looling at the amplitudes of
    the different non-zero samples.  The `boost_delta' argument identifies the
    differences between the minimum and maximum boost for samples within the
    block, while the `boost_mask' argument points to a two-dimensional array
    of values indicating the amount by which the actual boost for each
    sample exceeds the minimum boost.  The less significant samples have
    their magnitudes shifted up by `boost_delta' - `boost_mask[r][c]', where
    [r,c] is the index of the relevant sample within the code block. */

{
  ifc_int *sp, val, shift;
  std_byte *bp;
  int r, c;

  for (r=rows; r > 0; r--, boost_mask++, sample_buffer+=row_gap)
    for (sp=sample_buffer, bp=*boost_mask, c=cols; c > 0; c--, sp++, bp++)
      {
        shift = ((ifc_int) boost_delta) - ((ifc_int)(*bp));
        if (shift)
          {
            assert((shift >= 0) && (shift <= (ifc_int) boost_delta));
            val = *sp;
            val = (val & MIN_IFC_INT) | ((val & MAX_IFC_INT) << shift);
            *sp = val;
          }
      }
}

/*****************************************************************************/
/* STATIC              undo_roi_boost_using_implicit_mask                    */
/*****************************************************************************/

static void
  undo_roi_boost_using_implicit_mask(ifc_int *sample_buffer, int rows,
                                     int cols, int row_gap, int boost_delta)

 /* This function performs the same task as `undo_roi_boost_using_mask',
    except that the boosted samples are distinguished from those which
    have not been boosted implicitly by inspecting the sample magnitudes.
    Samples which have not been boosted must have the most significant
    `boost_delta' magnitude bits of their sign-magnitude representation
    equal to zero. */

{
  ifc_int *sp, val, shift, mask;
  int r, c;

  if ((boost_delta+1) >= IMPLEMENTATION_PRECISION)
    return; /* Background is too small to have been decoded at all with
               current implementation precision. */
  mask = (ifc_int)((-1) << (IMPLEMENTATION_PRECISION - (boost_delta+1)));
  mask &= MAX_IFC_INT; /* Remove sign bit. */
  shift = (ifc_int) boost_delta;
  for (r=rows; r > 0; r--, sample_buffer+=row_gap)
    for (sp=sample_buffer, c=cols; c > 0; c--, sp++)
      {
        val = *sp;
        if ((val & mask) == 0)
          { /* Must be a background sample. */
            val = (val & MIN_IFC_INT) | ((val & MAX_IFC_INT) << shift);
            *sp = val;
          }
      }
}

/*****************************************************************************/
/* STATIC                  deinterleave_sample_buffer                        */
/*****************************************************************************/

static void
  deinterleave_sample_buffer(block_master_ptr master, ifc_int *sample_buffer)

 /* Transfers the contents of the interleaved sample buffer to the
    supplied non-interleaved buffer, whose rows are separated by
    `master->sample_row_gap' array entries. */

{
  int cols, rows, r, c, stripe_r, row_gap;
  ifc_int *sp, *dp, *spp, *dpp;

  dpp = sample_buffer;
  spp = master->interleaved_sample_buffer;
  cols = master->width;
  rows = master->height;
  row_gap = master->sample_row_gap;
  for (stripe_r=4, r=rows; r > 0; r--, dpp+=row_gap, spp++, stripe_r--)
    {
      if (stripe_r == 0)
        { stripe_r=4; spp += master->interleaved_row_gap - 4; }
      for (sp=spp, dp=dpp, c=cols; c > 0; c--, sp+=4, dp++)
        *dp = *sp;
    }
}

/*****************************************************************************/
/* STATIC                     reset_block_contexts                           */
/*****************************************************************************/

static void
  reset_block_contexts(block_master_ptr master)

 /* Sets most context words to 0, except those corresponding to samples
    which might be accessed by the "slack" control loops in
    "ebcot_lite_decode_passes.c", which are explicitly marked here as
    being OUT_OF_BOUNDS.  This allows for somewhat faster execution of
    the decoding routines except on partial code-blocks which can only
    occur at image boundaries. */

{
  int rows, cols, c, d_gap;

⌨️ 快捷键说明

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