📄 ebcot_encoder.c
字号:
/*****************************************************************************/
/* Copyright 1998, Hewlett-Packard Company */
/* All rights reserved */
/* File: "ebcot_encoder.h" */
/* Description: Central source file for EBCOT encoder */
/* Author: David Taubman */
/* Affiliation: Hewlett-Packard and */
/* The University of New South Wales, Australia */
/* Version: VM9.0 */
/* Last Revised: 19 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 include masking-sensitive distortion */
/* calculations for R-D optimization (`-Cvis' option). Material identified */
/* by "David T Cvis mod" comments has been added by David Taubman; it is */
/* copyrighted by the University of New South Wales (Copyright 1999) with */
/* all rights reserved for the 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 to incorporate scan based rate control. Material identified by */
/* "CRIL Technology/SAIC Scan Buffer" comments has been added by Fabrice */
/* Pelleau and Olivier Duffez of CRIL Technology and Tom Flohr of SAIC. */
/* Copyright 2000 CRIL Technology */
/* Copyright 2000 CNES */
/* Copyright 2000 Science Applications International Corporation (SAIC). */
/* All Rights Reserved for 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". */
/* Generally, any reference to tile-parts (e.g. through variable names */
/* containing the string `tpart' are part of this modification, since the */
/* compressor did not previously need to be tile-part aware. */
/* 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 <time.h>
#include <assert.h>
#include <ifc.h>
#include <markers.h>
#include "ebcot_common.h"
#include "ebcot_encoder.h"
#include "ebcot_send_bits.h"
#include "ebcot_vpw.h"
/* ========================================================================= */
/* --------------------------- 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,"wb");
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 */
/*****************************************************************************/
/* STATIC allocate_pass_info */
/*****************************************************************************/
static void
allocate_pass_info(ebcot_band_info_ptr band)
/* This function allocates the `pass_info' array for the subband and
sets the `passes' field of each code block in the subband to point to
the relevant portion of this array. To do this, the function invokes
the `forward_roi__get_max_boost' function, as necessary, to determine
the maximum number of passes which might have to be generated for
any given code block. */
{
ebcot_pass_info_ptr passes;
ebcot_block_info_ptr block;
int block_aligned, max_passes, max_planes, block_idx;
assert(band->roi != NULL);
band->max_roi_boost =
band->roi->get_max_boost(band->roi,band->component_idx,band->level_idx,
band->band_idx,&block_aligned,
&(band->roi_implicit));
if (band->max_roi_boost == 0)
band->roi = NULL; /* No need to call `forward_roi__check_roi'. */
if (block_aligned)
band->max_roi_boost = 0;
max_planes = IMPLEMENTATION_PRECISION - 1 - band->extra_lsbs;
max_planes += band->max_roi_boost;
if (max_planes > (IMPLEMENTATION_PRECISION-2))
max_planes = IMPLEMENTATION_PRECISION-2;
max_passes = 1 + PASSES_PER_BITPLANE*(max_planes-1);
/* Sharp Labs change below */
if ( max_planes < 0 )
max_planes = 0;
if ( max_passes < 0 )
max_passes = 0;
band->max_passes = max_passes;
passes = (ebcot_pass_info_ptr)
local_malloc(EBCOT_MEM_KEY,
sizeof(ebcot_pass_info)*(band->total_blocks*max_passes));
for (block_idx=0; block_idx < band->total_blocks; block_idx++)
{
block = band->blocks + block_idx;
block->max_passes = 0; /* Fill this in at encode time. */
block->passes = passes;
passes += max_passes;
}
}
/* ========================================================================= */
/* ---------------------- Rate-Distortion Functions ------------------------ */
/* ========================================================================= */
/*****************************************************************************/
/* STATIC convert_double_to_rd_slope_type */
/*****************************************************************************/
static rd_slope_type
convert_double_to_rd_slope_type(double slope)
/* Converts a real-valued rate-distortion slope to the exponent-mantissa
representation associated with the `rd_slope_type' data-type, as
explained with its definition in "ebcot_encoder.h". Excessively
small or large slope values are truncated to the minimum and maximum
legal slopes, respectively. */
{
std_short exponent, max_exponent, mantissa, max_mantissa;
double boundary;
max_exponent = (1<<RD_SLOPE_EXPONENT_BITS) - 1;
max_mantissa = (1<<RD_SLOPE_MANTISSA_BITS) - 1;
boundary = 2.0 - 0.5 / ((double)(max_mantissa+1));
/* Largest value which should be represented by an exponent of 0. */
for (exponent=0; exponent < (max_exponent-1); exponent++, slope*=0.5)
if (slope <= boundary)
break;
if (slope > boundary)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -