📄 cabac.c
字号:
************************************************************************
*/
int read_and_store_CBP_block_bit (Macroblock *currMB,
DecodingEnvironmentPtr dep_dp,
struct img_par *img,
int type)
{
int y_ac = (type==LUMA_16AC || type==LUMA_8x8 || type==LUMA_8x4 || type==LUMA_4x8 || type==LUMA_4x4);
int y_dc = (type==LUMA_16DC);
int u_ac = (type==CHROMA_AC && !img->is_v_block);
int v_ac = (type==CHROMA_AC && img->is_v_block);
int chroma_dc = (type==CHROMA_DC || type==CHROMA_DC_2x4 || type==CHROMA_DC_4x4);
int u_dc = (chroma_dc && !img->is_v_block);
int v_dc = (chroma_dc && img->is_v_block);
int j = (y_ac || u_ac || v_ac ? img->subblock_y : 0);
int i = (y_ac || u_ac || v_ac ? img->subblock_x : 0);
int bit = (y_dc ? 0 : y_ac ? 1 : u_dc ? 17 : v_dc ? 18 : u_ac ? 19 : 35);
int default_bit = (img->is_intra_block ? 1 : 0);
int upper_bit = default_bit;
int left_bit = default_bit;
int cbp_bit = 1; // always one for 8x8 mode
int ctx;
int bit_pos_a = 0;
int bit_pos_b = 0;
PixelPos block_a, block_b;
if (y_ac || y_dc)
{
getLuma4x4Neighbour(img->current_mb_nr, i, j, -1, 0, &block_a);
getLuma4x4Neighbour(img->current_mb_nr, i, j, 0, -1, &block_b);
if (y_ac)
{
if (block_a.available)
bit_pos_a = 4*block_a.y + block_a.x;
if (block_b.available)
bit_pos_b = 4*block_b.y + block_b.x;
}
}
else
{
getChroma4x4Neighbour(img->current_mb_nr, i, j, -1, 0, &block_a);
getChroma4x4Neighbour(img->current_mb_nr, i, j, 0, -1, &block_b);
if (u_ac||v_ac)
{
if (block_a.available)
bit_pos_a = 4*block_a.y + block_a.x;
if (block_b.available)
bit_pos_b = 4*block_b.y + block_b.x;
}
}
if (type!=LUMA_8x8)
{
//--- get bits from neighbouring blocks ---
if (block_b.available)
{
if(img->mb_data[block_b.mb_addr].mb_type==IPCM)
upper_bit=1;
else
upper_bit = BIT_SET(img->mb_data[block_b.mb_addr].cbp_bits,bit+bit_pos_b);
}
if (block_a.available)
{
if(img->mb_data[block_a.mb_addr].mb_type==IPCM)
left_bit=1;
else
left_bit = BIT_SET(img->mb_data[block_a.mb_addr].cbp_bits,bit+bit_pos_a);
}
ctx = 2*upper_bit+left_bit;
//===== encode symbol =====
cbp_bit = biari_decode_symbol (dep_dp, img->currentSlice->tex_ctx->bcbp_contexts[type2ctx_bcbp[type]] + ctx);
}
//--- set bits for current block ---
bit = (y_dc ? 0 : y_ac ? 1+4*j+i : u_dc ? 17 : v_dc ? 18 : u_ac ? 19+4*j+i : 35+4*j+i);
if (cbp_bit)
{
if (type==LUMA_8x8)
{
currMB->cbp_bits |= ((int64)1<< bit );
currMB->cbp_bits |= ((int64)1<<(bit+1));
currMB->cbp_bits |= ((int64)1<<(bit+4));
currMB->cbp_bits |= ((int64)1<<(bit+5));
}
else if (type==LUMA_8x4)
{
currMB->cbp_bits |= ((int64)1<< bit );
currMB->cbp_bits |= ((int64)1<<(bit+1));
}
else if (type==LUMA_4x8)
{
currMB->cbp_bits |= ((int64)1<< bit );
currMB->cbp_bits |= ((int64)1<<(bit+4));
}
else
{
currMB->cbp_bits |= ((int64)1<<bit);
}
}
return cbp_bit;
}
//===== position -> ctx for MAP =====
//--- zig-zag scan ----
static const int pos2ctx_map8x8 [] = { 0, 1, 2, 3, 4, 5, 5, 4, 4, 3, 3, 4, 4, 4, 5, 5,
4, 4, 4, 4, 3, 3, 6, 7, 7, 7, 8, 9, 10, 9, 8, 7,
7, 6, 11, 12, 13, 11, 6, 7, 8, 9, 14, 10, 9, 8, 6, 11,
12, 13, 11, 6, 9, 14, 10, 9, 11, 12, 13, 11 ,14, 10, 12, 14}; // 15 CTX
static const int pos2ctx_map8x4 [] = { 0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 9, 8, 6, 7, 8,
9, 10, 11, 9, 8, 6, 12, 8, 9, 10, 11, 9, 13, 13, 14, 14}; // 15 CTX
static const int pos2ctx_map4x4 [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14}; // 15 CTX
static const int pos2ctx_map2x4c[] = { 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}; // 15 CTX
static const int pos2ctx_map4x4c[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2}; // 15 CTX
#ifdef USE_INTRA_MDDT
static const int pos2ctx_map16x16[]= {
0, 1, 2, 2, 1, 1, 1, 1, 2, 2, 3, 2, 4, 1, 5, 5,
5, 4, 4, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 4, 4,
4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 4,
4, 4, 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3,
6, 4, 7, 4, 7, 4, 7, 4, 8, 5, 9, 9, 9, 8, 8, 7,
7, 7, 7, 7, 7, 6, 6, 3, 3, 12, 3, 11, 6, 6, 7, 7,
7, 7, 7, 8, 8, 9, 9, 10, 10, 10, 9, 9, 8, 8, 7, 7,
7, 7, 6, 6, 11, 11, 12, 12, 12, 13, 11, 11, 6, 6, 7, 7,
7, 8, 8, 9, 9, 14, 10, 14, 14, 9, 9, 8, 8, 7, 7, 6,
6, 11, 11, 13, 13, 13, 12, 11, 11, 6, 6, 7, 8, 8, 9, 9,
10, 14, 10, 10, 9, 9, 8, 8, 6, 6, 11, 11, 12, 12, 12, 13,
11, 11, 6, 6, 8, 9, 9, 14, 10, 14, 14, 9, 9, 6, 6, 11,
11, 13, 13, 13, 12, 11, 11, 6, 9, 9, 10, 14, 10, 10, 9, 9,
11, 11, 12, 12, 12, 13, 11, 11, 9, 14, 10, 14, 14, 11, 11, 13,
#ifdef RDO_Q
13, 13, 12, 11, 10, 14, 10, 10, 12, 12, 12, 14, 10, 14, 14, 15,
#else
13, 13, 12, 11, 10, 14, 10, 10, 12, 12, 12, 14, 10, 14, 14, 14,
#endif
}; // 15 CTX
static const int* pos2ctx_map [] = {pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8, pos2ctx_map8x4,
pos2ctx_map8x4, pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map4x4,
pos2ctx_map2x4c, pos2ctx_map4x4c,pos2ctx_map16x16};
#else
static const int* pos2ctx_map [] = {pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8, pos2ctx_map8x4,
pos2ctx_map8x4, pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map4x4,
pos2ctx_map2x4c, pos2ctx_map4x4c};
#endif
//--- interlace scan ----
//taken from ABT
static const int pos2ctx_map8x8i[] = { 0, 1, 1, 2, 2, 3, 3, 4, 5, 6, 7, 7, 7, 8, 4, 5,
6, 9, 10, 10, 8, 11, 12, 11, 9, 9, 10, 10, 8, 11, 12, 11,
9, 9, 10, 10, 8, 11, 12, 11, 9, 9, 10, 10, 8, 13, 13, 9,
9, 10, 10, 8, 13, 13, 9, 9, 10, 10, 14, 14, 14, 14, 14, 14}; // 15 CTX
static const int pos2ctx_map8x4i[] = { 0, 1, 2, 3, 4, 5, 6, 3, 4, 5, 6, 3, 4, 7, 6, 8,
9, 7, 6, 8, 9, 10, 11, 12, 12, 10, 11, 13, 13, 14, 14, 14}; // 15 CTX
static const int pos2ctx_map4x8i[] = { 0, 1, 1, 1, 2, 3, 3, 4, 4, 4, 5, 6, 2, 7, 7, 8,
8, 8, 5, 6, 9, 10, 10, 11, 11, 11, 12, 13, 13, 14, 14, 14}; // 15 CTX
static const int* pos2ctx_map_int[] = {pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8i,pos2ctx_map8x4i,
pos2ctx_map4x8i,pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map4x4,
pos2ctx_map2x4c, pos2ctx_map4x4c};
//===== position -> ctx for LAST =====
static const int pos2ctx_last8x8 [] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8}; // 9 CTX
static const int pos2ctx_last8x4 [] = { 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8}; // 9 CTX
static const int pos2ctx_last4x4 [] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; // 15 CTX
static const int pos2ctx_last2x4c[] = { 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}; // 15 CTX
static const int pos2ctx_last4x4c[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2}; // 15 CTX
#ifdef USE_INTRA_MDDT
static const int pos2ctx_last16x16[]= {
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 3, 2, 3, 2, 3,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2, 4, 2, 4, 2, 4, 2, 4, 4, 4, 4, 4, 4, 3, 3, 3,
3, 3, 3, 3, 3, 3, 5, 3, 4, 3, 4, 3, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5,
4, 5, 4, 5, 4, 6, 4, 6, 4, 6, 6, 6, 6, 5, 5, 5,
5, 5, 5, 5, 7, 5, 7, 5, 6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7, 6, 8, 6, 8, 8, 7, 7, 7,
#ifdef RDO_Q
7, 7, 8, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 15,
#else
7, 7, 8, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
#endif
}; // 9 CTX
static const int* pos2ctx_last [] = {pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last8x8, pos2ctx_last8x4,
pos2ctx_last8x4, pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last4x4,
pos2ctx_last2x4c, pos2ctx_last4x4c, pos2ctx_last16x16};
#else
static const int* pos2ctx_last [] = {pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last8x8, pos2ctx_last8x4,
pos2ctx_last8x4, pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last4x4,
pos2ctx_last2x4c, pos2ctx_last4x4c};
#endif
/*!
************************************************************************
* \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 || currMB->mb_field)
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, max_c2[type]);
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;
}
}
}
}
#ifdef ADAPTIVE_FD_SD_CODING
void readRunLevel_CABAC (SyntaxElement *se,
struct inp_par *inp,
struct img_par *img,
DecodingEnvironmentPtr dep_dp)
{
#ifdef USE_INTRA_MDDT
static int coeff[256]; // one more for EOB
#else
static int coeff[64]; // one more for EOB
#endif
static int coeff_ctr = -1;
static int pos = 0;
int DF_or_SD=0;
int i;
int writing_b8=min(3,max(0,se->writing_b8));
int writing_b4=min(3,max(0,se->writing_b4));
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)))
{
if (currMB->SD_Coding_on_off==1 && img->Allow_SD_Coding && currMB->b8mode[writing_b8] != IBLOCK && currMB->luma_transform_size_8x8_flag==0 && se->context==LUMA_4x4 && !IS_INTRA (currMB) )
{
DF_or_SD = read_spatial_domain_coding_flag_for_4x4block (currMB, dep_dp, img, se->context);
if (DF_or_SD)
{
//===== decode significance map =====
coeff_ctr = read_significance_map_SD (currMB, dep_dp, img, se->context, coeff);
//===== decode significant coefficients =====
read_significant_coefficients_SD (currMB, dep_dp, img, se->context, coeff);
for (i=0; i<16; i++)
{
currMB->quantizer_indices[((writing_b8/ 2)<<3)+((writing_b4/ 2)<<2)+(i/4)][((writing_b8% 2)<<3)+((writing_b4% 2)<<2)+(i%4)]=coeff[i];
}
currMB->SD_or_FD[writing_b8/ 2][writing_b8% 2]+=(1<<writing_b4);
}
else
{
//===== 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);
}
}
else if (currMB->SD_Coding_on_off==1 && img->Allow_SD_Coding && currMB->b8mode[writing_b8] != IBLOCK && currMB->luma_transform_size_8x8_flag==1 && se->context==LUMA_8x8 && !IS_INTRA (currMB) )
{
if ((DF_or_SD = read_spatial_domain_coding_flag_for_8x8block (currMB, dep_dp, img, se->context,writing_b8)))
{
//===== decode significance map =====
coeff_ctr = read_significance_map_SD8 (currMB, dep_dp, img, se->context, coeff);
//===== decode significant coefficients =====
read_significant_coefficients_SD8 (currMB, dep_dp, img, se->context, coeff);
for (i=0; i<64; i++)
{
currMB->quantizer_indices[((writing_b8/ 2)<<3)+(i/8)][((writing_b8% 2)<<3)+(i%8)]=coeff[i];
}
currMB->SD_or_FD_t8x8+=(1<<writing_b8);
}
else
{
//===== 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);
}
}
else
{
//===== 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 ---
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -