📄 ebcot_lite_encode_passes.c
字号:
/*****************************************************************************/
/* File name: "ebcot_lite_encode_passes.c" */
/* Author: David Taubman */
/* Copyright 1998, Hewlett-Packard Company */
/* All rights reserved */
/*****************************************************************************/
#include <local_heap.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <line_block_ifc.h>
#include <ebcot_common.h>
#include <ebcot_encoder.h>
#include <ebcot_arith_encoder.h>
/* ========================================================================= */
/* --------------------------- Internal Functions -------------------------- */
/* ========================================================================= */
/*****************************************************************************/
/* STATIC first_pass_func */
/*****************************************************************************/
static std_int
first_pass_func(block_master_ptr master)
/* This function must be applied once, at the start of each quantization
layer (bit plane). It looks for all samples whose context word has the
IS_REFINED flag turned off. These samples are brought up to date with
the current bit plane, while all other samples simply have their
IS_REFINED flag turned off in preparation for future passes. Note that
bringing samples up to date with the current bit plane never involves
magnitude refinement. */
{
register std_int *d_cp;
register std_short *cp;
register ifc_int *dp;
register int c;
register std_short ctxt;
register ifc_int mask;
register arith_state_ptr state;
register std_int A, C, word, rho;
register context_state_ptr csp;
register ifc_int val, symbol;
std_short avail;
ifc_int lsb, shift;
std_byte *zc_lut;
ifc_int mse_upshift, mse_downshift;
std_int delta_mse_acc;
int rows, cols, row_gap, half_inter_row, r;
std_short *context;
ifc_int *data;
context_state_ptr csp_base;
code_subblock_ptr subblock, last_subblock;
shift = master->bit_idx;
assert(shift > 0);
lsb = 1 << shift;
mask = (std_short)(MAX_IFC_INT & ((-1)<<shift));
data = master->sample_buffer;
context = master->context_buffer;
row_gap = master->row_gap;
assert(!(row_gap & 1));
delta_mse_acc = 0;
mse_downshift = shift - (MSE_LUT_BITS-1);
mse_upshift = (mse_downshift<0)?-mse_downshift:0;
mse_downshift += mse_upshift;
state = &(master->coder_state);
A=state->A; C=state->C; word=state->word; rho=state->rho;
avail=state->available_bits; csp_base = state->contexts;
zc_lut = master->zc_lut;
last_subblock = master->last_subblock;
ctxt = 0; /* Suppress compiler warnings. */
for (subblock=master->first_subblock; subblock <= last_subblock; subblock++)
if (subblock->significant)
{
rows = subblock->rows;
cols = subblock->cols;
half_inter_row = (row_gap-cols)>>1;
dp = data + subblock->offset;
cp = context + subblock->offset;
d_cp = (std_int *) cp;
for (r=rows; r > 0; r--, dp+=half_inter_row<<1, cp+=half_inter_row<<1,
d_cp+=half_inter_row)
for (c=cols; c > 0; c-=2, dp+=2, cp+=2, d_cp++)
{
if ((d_cp[0] == 0) && (d_cp[1] == 0) && (c > 2))
{ /* Special processing to reduce average symbol count. */
symbol = 0;
if (dp[0] & mask)
{ ctxt = 0; symbol = 1; }
else if (dp[1] & mask)
{ ctxt = 1; symbol = 1; }
else if (dp[2] & mask)
{ ctxt = 2; symbol = 1; }
else if (dp[3] & mask)
{ ctxt = 3; symbol = 1; }
csp = csp_base + AGG_OFFSET;
emit_symbol(state,A,C,word,avail,rho,symbol,csp);
if (!symbol)
{ c -= 2; dp += 2; cp += 2; d_cp++; continue; }
else
{ /* Send the run-length, stored in `ctxt', code the sign
bit, and jump into the right location to ensure that
we move on from the end of the run. */
ifc_int sign;
val = dp[ctxt];
symbol = ctxt & 2;
emit_uniform_symbol(state,A,C,word,avail,rho,symbol);
symbol = ctxt & 1;
emit_uniform_symbol(state,A,C,word,avail,rho,symbol);
sign = val & MIN_IFC_INT; /* Save sign bit. */
val &= MAX_IFC_INT;
val >>= mse_downshift; val <<= mse_upshift;
assert(val == (val & MSE_LUT_MASK));
delta_mse_acc += ebcot_initial_mse_lut[val];
emit_uniform_symbol(state,A,C,word,avail,rho,sign);
cp += ctxt;
cp[0] = SELF_SIG;
cp[-row_gap-2] |= FAR_SIG;
cp[-row_gap-1] |= BR_SIG;
cp[-row_gap+1] |= BL_SIG;
cp[-row_gap+2] |= FAR_SIG;
cp[row_gap-2] |= FAR_SIG;
cp[row_gap-1] |= TR_SIG;
cp[row_gap+1] |= TL_SIG;
cp[row_gap+2] |= FAR_SIG;
cp[-2] |= FAR_SIG;
cp[2] |= FAR_SIG;
if (sign)
{ /* Negative sample. */
cp[-1] |= CR_SIG | H_NVE_SIG;
cp[1] |= CL_SIG | H_NVE_SIG;
cp[-row_gap] |= BC_SIG | V_NVE_SIG;
cp[row_gap] |= TC_SIG | V_NVE_SIG;
}
else
{ /* Positive sample. */
cp[-1] |= CR_SIG | H_PVE_SIG;
cp[1] |= CL_SIG | H_PVE_SIG;
cp[-row_gap] |= BC_SIG | V_PVE_SIG;
cp[row_gap] |= TC_SIG | V_PVE_SIG;
}
cp -= ctxt;
if (ctxt == 0)
goto skip1;
else if (ctxt == 3)
{ c -= 2; dp += 2; cp += 2; d_cp++; continue; }
else
continue;
}
}
ctxt = cp[0];
if (ctxt & IS_REFINED)
cp[0] = ctxt & ~IS_REFINED;
else if (!(ctxt & (SELF_SIG | OUT_OF_BOUNDS)))
{
val = dp[0];
symbol = (val & mask);
csp = csp_base + (ZC_OFFSET + zc_lut[ctxt & ZC_MASK]);
emit_symbol(state,A,C,word,avail,rho,symbol,csp);
if (symbol)
{ /* New significant value; update contexts & code sign */
ifc_int sign;
sign = val & MIN_IFC_INT; /* Save sign bit. */
val &= MAX_IFC_INT;
val >>= mse_downshift; val <<= mse_upshift;
assert(val == (val & MSE_LUT_MASK));
delta_mse_acc += ebcot_initial_mse_lut[val];
symbol = ebcot_sc_lut[(ctxt>>SIGN_POS)&0x00FF];
csp = csp_base + SC_OFFSET + (symbol & 0x000F);
symbol &= MIN_IFC_INT;
symbol ^= sign;
emit_symbol(state,A,C,word,avail,rho,symbol,csp);
cp[0] = ctxt | SELF_SIG;
cp[-row_gap-2+0] |= FAR_SIG;
cp[-row_gap-1+0] |= BR_SIG;
cp[-row_gap+1+0] |= BL_SIG;
cp[-row_gap+2+0] |= FAR_SIG;
cp[row_gap-2+0] |= FAR_SIG;
cp[row_gap-1+0] |= TR_SIG;
cp[row_gap+1+0] |= TL_SIG;
cp[row_gap+2+0] |= FAR_SIG;
cp[-2+0] |= FAR_SIG;
cp[2+0] |= FAR_SIG;
if (sign)
{ /* Negative sample. */
cp[-1+0] |= CR_SIG | H_NVE_SIG;
cp[1+0] |= CL_SIG | H_NVE_SIG;
cp[-row_gap+0] |= BC_SIG | V_NVE_SIG;
cp[row_gap+0] |= TC_SIG | V_NVE_SIG;
}
else
{ /* Positive sample. */
cp[-1+0] |= CR_SIG | H_PVE_SIG;
cp[1+0] |= CL_SIG | H_PVE_SIG;
cp[-row_gap+0] |= BC_SIG | V_PVE_SIG;
cp[row_gap+0] |= TC_SIG | V_PVE_SIG;
}
}
}
skip1:
ctxt = cp[1];
if (ctxt & IS_REFINED)
cp[1] = ctxt & ~IS_REFINED;
else if (!(ctxt & (SELF_SIG | OUT_OF_BOUNDS)))
{
val = dp[1];
symbol = (val & mask);
csp = csp_base + (ZC_OFFSET + zc_lut[ctxt & ZC_MASK]);
emit_symbol(state,A,C,word,avail,rho,symbol,csp);
if (symbol)
{ /* New significant value; update contexts & code sign */
ifc_int sign;
sign = val & MIN_IFC_INT; /* Save sign bit. */
val &= MAX_IFC_INT;
val >>= mse_downshift; val <<= mse_upshift;
assert(val == (val & MSE_LUT_MASK));
delta_mse_acc += ebcot_initial_mse_lut[val];
symbol = ebcot_sc_lut[(ctxt>>SIGN_POS)&0x00FF];
csp = csp_base + SC_OFFSET + (symbol & 0x000F);
symbol &= MIN_IFC_INT;
symbol ^= sign;
emit_symbol(state,A,C,word,avail,rho,symbol,csp);
cp[1] = ctxt | SELF_SIG;
cp[-row_gap-2+1] |= FAR_SIG;
cp[-row_gap-1+1] |= BR_SIG;
cp[-row_gap+1+1] |= BL_SIG;
cp[-row_gap+2+1] |= FAR_SIG;
cp[row_gap-2+1] |= FAR_SIG;
cp[row_gap-1+1] |= TR_SIG;
cp[row_gap+1+1] |= TL_SIG;
cp[row_gap+2+1] |= FAR_SIG;
cp[-2+1] |= FAR_SIG;
cp[2+1] |= FAR_SIG;
if (sign)
{ /* Negative sample. */
cp[-1+1] |= CR_SIG | H_NVE_SIG;
cp[1+1] |= CL_SIG | H_NVE_SIG;
cp[-row_gap+1] |= BC_SIG | V_NVE_SIG;
cp[row_gap+1] |= TC_SIG | V_NVE_SIG;
}
else
{ /* Positive sample. */
cp[-1+1] |= CR_SIG | H_PVE_SIG;
cp[1+1] |= CL_SIG | H_PVE_SIG;
cp[-row_gap+1] |= BC_SIG | V_PVE_SIG;
cp[row_gap+1] |= TC_SIG | V_PVE_SIG;
}
}
}
}
}
state->A=A; state->C=C; state->word=word; state->rho=rho;
state->available_bits=avail;
return(delta_mse_acc);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -