📄 cabac.c
字号:
/*!
************************************************************************
* \brief
* Read Significance MAP
************************************************************************
*/
int read_significance_map (Macroblock *currMB,
DecodingEnvironmentPtr dep_dp,
struct img_par *img,
int type,
int coeff[])
{
int i, sig;
int coeff_ctr = 0;
int i0 = 0;
int i1 = maxpos[type]-1;
int fld = ( img->structure!=FRAME || currMB->mb_field );
BiContextTypePtr map_ctx = ( fld ? img->currentSlice->tex_ctx-> fld_map_contexts[type2ctx_map [type]]
: img->currentSlice->tex_ctx-> map_contexts[type2ctx_map [type]] );
BiContextTypePtr last_ctx = ( fld ? img->currentSlice->tex_ctx->fld_last_contexts[type2ctx_last[type]]
: img->currentSlice->tex_ctx-> last_contexts[type2ctx_last[type]] );
if (!c1isdc[type])
{
i0++; i1++; coeff--;
}
for (i=i0; i<i1; i++) // if last coeff is reached, it has to be significant
{
//--- read significance symbol ---
if (img->structure!=FRAME)
sig = biari_decode_symbol (dep_dp, map_ctx + pos2ctx_map_int [type][i]);
else
sig = biari_decode_symbol (dep_dp, map_ctx + pos2ctx_map [type][i]);
if (sig)
{
coeff[i] = 1;
coeff_ctr++;
//--- read last coefficient symbol ---
if (biari_decode_symbol (dep_dp, last_ctx + pos2ctx_last[type][i]))
{
for (i++; i<i1+1; i++) coeff[i] = 0;
}
}
else
{
coeff[i] = 0;
}
}
//--- last coefficient must be significant if no last symbol was received ---
if (i<i1+1)
{
coeff[i] = 1;
coeff_ctr++;
}
return coeff_ctr;
}
/*!
************************************************************************
* \brief
* Read Levels
************************************************************************
*/
void read_significant_coefficients (Macroblock *currMB,
DecodingEnvironmentPtr dep_dp,
struct img_par *img,
int type,
int coeff[])
{
int i, ctx;
int c1 = 1;
int c2 = 0;
for (i=maxpos[type]-1; i>=0; i--)
{
if (coeff[i]!=0)
{
ctx = min (c1,4);
coeff[i] += biari_decode_symbol (dep_dp, img->currentSlice->tex_ctx->one_contexts[type2ctx_one[type]] + ctx);
if (coeff[i]==2)
{
ctx = min (c2,4);
coeff[i] += unary_exp_golomb_level_decode (dep_dp, img->currentSlice->tex_ctx->abs_contexts[type2ctx_abs[type]]+ctx);
c1=0;
c2++;
}
else if (c1)
{
c1++;
}
if (biari_decode_symbol_eq_prob(dep_dp))
{
coeff[i] *= -1;
}
}
}
}
/*!
************************************************************************
* \brief
* Read Block-Transform Coefficients
************************************************************************
*/
void readRunLevel_CABAC (SyntaxElement *se,
struct inp_par *inp,
struct img_par *img,
DecodingEnvironmentPtr dep_dp)
{
static int coeff[64]; // one more for EOB
static int coeff_ctr = -1;
static int pos = 0;
Macroblock *currMB = &img->mb_data[img->current_mb_nr];
//--- read coefficients for whole block ---
if (coeff_ctr < 0)
{
//===== decode CBP-BIT =====
if ((coeff_ctr = read_and_store_CBP_block_bit (currMB, dep_dp, img, se->context)))
{
//===== decode significance map =====
coeff_ctr = read_significance_map (currMB, dep_dp, img, se->context, coeff);
//===== decode significant coefficients =====
read_significant_coefficients (currMB, dep_dp, img, se->context, coeff);
}
}
//--- set run and level ---
if (coeff_ctr)
{
//--- set run and level (coefficient) ---
for (se->value2=0; coeff[pos]==0; pos++, se->value2++);
se->value1=coeff[pos++];
}
else
{
//--- set run and level (EOB) ---
se->value1 = se->value2 = 0;
}
//--- decrement coefficient counter and re-set position ---
if (coeff_ctr-- == 0) pos=0;
#if TRACE
fprintf(p_trace, "@%d %s\t\t\t%d\t%d\n",symbolCount++, se->tracestring, se->value1,se->value2);
fflush(p_trace);
#endif
}
/*!
************************************************************************
* \brief
* arithmetic decoding
************************************************************************
*/
int readSyntaxElement_CABAC(SyntaxElement *se, struct img_par *img, struct inp_par *inp, DataPartition *this_dataPart)
{
int curr_len;
DecodingEnvironmentPtr dep_dp = &(this_dataPart->de_cabac);
curr_len = arideco_bits_read(dep_dp);
// perform the actual decoding by calling the appropriate method
se->reading(se, inp, img, dep_dp);
return (se->len = (arideco_bits_read(dep_dp) - curr_len));
}
/*!
************************************************************************
* \brief
* decoding of unary binarization using one or 2 distinct
* models for the first and all remaining bins; no terminating
* "0" for max_symbol
***********************************************************************
*/
unsigned int unary_bin_max_decode(DecodingEnvironmentPtr dep_dp,
BiContextTypePtr ctx,
int ctx_offset,
unsigned int max_symbol)
{
unsigned int l;
unsigned int symbol;
BiContextTypePtr ictx;
symbol = biari_decode_symbol(dep_dp, ctx );
if (symbol==0)
return 0;
else
{
if (max_symbol == 1)
return symbol;
symbol=0;
ictx=ctx+ctx_offset;
do
{
l=biari_decode_symbol(dep_dp, ictx);
symbol++;
}
while( (l!=0) && (symbol<max_symbol-1) );
if ((l!=0) && (symbol==max_symbol-1))
symbol++;
return symbol;
}
}
/*!
************************************************************************
* \brief
* decoding of unary binarization using one or 2 distinct
* models for the first and all remaining bins
***********************************************************************
*/
unsigned int unary_bin_decode(DecodingEnvironmentPtr dep_dp,
BiContextTypePtr ctx,
int ctx_offset)
{
unsigned int l;
unsigned int symbol;
BiContextTypePtr ictx;
symbol = biari_decode_symbol(dep_dp, ctx );
if (symbol==0)
return 0;
else
{
symbol=0;
ictx=ctx+ctx_offset;
do
{
l=biari_decode_symbol(dep_dp, ictx);
symbol++;
}
while( l!=0 );
return symbol;
}
}
/*!
************************************************************************
* \brief
* finding end of a slice in case this is not the end of a frame
*
* Unsure whether the "correction" below actually solves an off-by-one
* problem or whether it introduces one in some cases :-( Anyway,
* with this change the bit stream format works with CABAC again.
* StW, 8.7.02
************************************************************************
*/
int cabac_startcode_follows(struct img_par *img, struct inp_par *inp, int eos_bit)
{
Slice *currSlice = img->currentSlice;
int *partMap = assignSE2partition[currSlice->dp_mode];
DataPartition *dP;
unsigned int bit;
DecodingEnvironmentPtr dep_dp;
dP = &(currSlice->partArr[partMap[SE_MBTYPE]]);
dep_dp = &(dP->de_cabac);
if( eos_bit )
{
bit = biari_decode_final (dep_dp); //GB
#if TRACE
// strncpy(se->tracestring, "Decode Sliceterm", TRACESTRING_SIZE);
fprintf(p_trace, "@%d %s\t\t%d\n",symbolCount++, "Decode Sliceterm", bit);
fflush(p_trace);
#endif
}
else
{
bit = 0;
}
return (bit==1?1:0);
}
/*!
************************************************************************
* \brief
* Exp Golomb binarization and decoding of a symbol
* with prob. of 0.5
************************************************************************
*/
unsigned int exp_golomb_decode_eq_prob( DecodingEnvironmentPtr dep_dp,
int k)
{
unsigned int l;
int symbol = 0;
int binary_symbol = 0;
do
{
l=biari_decode_symbol_eq_prob(dep_dp);
if (l==1)
{
symbol += (1<<k);
k++;
}
}
while (l!=0);
while (k--) //next binary part
if (biari_decode_symbol_eq_prob(dep_dp)==1)
binary_symbol |= (1<<k);
return (unsigned int) (symbol+binary_symbol);
}
/*!
************************************************************************
* \brief
* Exp-Golomb decoding for LEVELS
***********************************************************************
*/
unsigned int unary_exp_golomb_level_decode( DecodingEnvironmentPtr dep_dp,
BiContextTypePtr ctx)
{
unsigned int l,k;
unsigned int symbol;
unsigned int exp_start = 13;
symbol = biari_decode_symbol(dep_dp, ctx );
if (symbol==0)
return 0;
else
{
symbol=0;
k=1;
do
{
l=biari_decode_symbol(dep_dp, ctx);
symbol++;
k++;
}
while((l!=0) && (k!=exp_start));
if (l!=0)
symbol += exp_golomb_decode_eq_prob(dep_dp,0)+1;
return symbol;
}
}
/*!
************************************************************************
* \brief
* Exp-Golomb decoding for Motion Vectors
***********************************************************************
*/
unsigned int unary_exp_golomb_mv_decode(DecodingEnvironmentPtr dep_dp,
BiContextTypePtr ctx,
unsigned int max_bin)
{
unsigned int l,k;
unsigned int bin=1;
unsigned int symbol;
unsigned int exp_start = 8;
BiContextTypePtr ictx=ctx;
symbol = biari_decode_symbol(dep_dp, ictx );
if (symbol==0)
return 0;
else
{
symbol=0;
k=1;
ictx++;
do
{
l=biari_decode_symbol(dep_dp, ictx );
if ((++bin)==2) ictx++;
if (bin==max_bin) ictx++;
symbol++;
k++;
}
while((l!=0) && (k!=exp_start));
if (l!=0)
symbol += exp_golomb_decode_eq_prob(dep_dp,3)+1;
return symbol;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -