📄 cabac.c
字号:
/*!
*************************************************************************************
* \file cabac.c
*
* \brief
* CABAC entropy coding routines
*
* \author
* Main contributors (see contributors.h for copyright, address and affiliation details)
* - Detlev Marpe <marpe@hhi.de>
**************************************************************************************
*/
#include "global.h"
#include "cabac.h"
#include "biariencode.h"
#include "image.h"
#include "mb_access.h"
#if TRACE
#define CABAC_TRACE if (dp->bitstream->trace_enabled) trace2out_cabac (se)
#else
#define CABAC_TRACE
#endif
/***********************************************************************
Constant declarations
***********************************************************************
*/
const byte maxpos [] = {15, 14, 63, 31, 31, 15, 3, 14, 7, 15, 15, 14, 63, 31, 31, 15, 15, 14, 63, 31, 31, 15};
const byte c1isdc [] = { 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1};
const byte type2ctx_bcbp[] = { 0, 1, 2, 3, 3, 4, 5, 6, 5, 5, 10, 11, 12, 13, 13, 14, 16, 17, 18, 19, 19, 20};
const byte type2ctx_map [] = { 0, 1, 2, 3, 4, 5, 6, 7, 6, 6, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}; // 8
const byte type2ctx_last[] = { 0, 1, 2, 3, 4, 5, 6, 7, 6, 6, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}; // 8
const byte type2ctx_one [] = { 0, 1, 2, 3, 3, 4, 5, 6, 5, 5, 10, 11, 12, 13, 13, 14, 16, 17, 18, 19, 19, 20}; // 7
const byte type2ctx_abs [] = { 0, 1, 2, 3, 3, 4, 5, 6, 5, 5, 10, 11, 12, 13, 13, 14, 16, 17, 18, 19, 19, 20}; // 7
const byte max_c2 [] = { 4, 4, 4, 4, 4, 4, 3, 4, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4}; // 9
//===== position -> ctx for MAP =====
//--- zig-zag scan ----
const byte 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
const byte 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
const byte pos2ctx_map4x4 [] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14
}; // 15 CTX
const byte pos2ctx_map2x4c[] = {
0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
}; // 15 CTX
const byte pos2ctx_map4x4c[] = {
0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2
}; // 15 CTX
const byte* pos2ctx_map [] = {
pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8, pos2ctx_map8x4,
pos2ctx_map8x4, pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map4x4,
pos2ctx_map2x4c, pos2ctx_map4x4c,
pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8,pos2ctx_map8x4,
pos2ctx_map8x4, pos2ctx_map4x4, //Cb component
pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8,pos2ctx_map8x4,
pos2ctx_map8x4,pos2ctx_map4x4 //Cr component
};
//--- interlace scan ----
//Taken from ABT
const byte 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
const byte 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
const byte 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
const byte* pos2ctx_map_int[] = {
pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8i,pos2ctx_map8x4i,
pos2ctx_map4x8i,pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map4x4,
pos2ctx_map2x4c, pos2ctx_map4x4c,
//444_TEMP_NOTE: the followings are addded for the 4:4:4 common mode};
pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8i,pos2ctx_map8x4i,
pos2ctx_map8x4i,pos2ctx_map4x4, //Cb component
pos2ctx_map4x4, pos2ctx_map4x4, pos2ctx_map8x8i,pos2ctx_map8x4i,
pos2ctx_map8x4i,pos2ctx_map4x4 //Cr component}
};
//===== position -> ctx for LAST =====
const byte 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
const byte 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
const byte pos2ctx_last4x4 [] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
}; // 15 CTX
const byte pos2ctx_last2x4c[] = {
0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
}; // 15 CTX
const byte pos2ctx_last4x4c[] = {
0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2
}; // 15 CTX
const byte* pos2ctx_last [] = {
pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last8x8, pos2ctx_last8x4,
pos2ctx_last8x4, pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last4x4,
pos2ctx_last2x4c, pos2ctx_last4x4c,
pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last8x8,pos2ctx_last8x4,
pos2ctx_last8x4, pos2ctx_last4x4, //Cb component
pos2ctx_last4x4, pos2ctx_last4x4, pos2ctx_last8x8,pos2ctx_last8x4,
pos2ctx_last8x4, pos2ctx_last4x4 //Cr component
};
/***********************************************************************
* L O C A L L Y D E F I N E D F U N C T I O N P R O T O T Y P E S
***********************************************************************
*/
static void unary_bin_encode(EncodingEnvironmentPtr eep_frame,
unsigned int symbol,
BiContextTypePtr ctx,
int ctx_offset);
static void unary_bin_max_encode(EncodingEnvironmentPtr eep_frame,
unsigned int symbol,
BiContextTypePtr ctx,
int ctx_offset,
unsigned int max_symbol);
static void unary_exp_golomb_level_encode( EncodingEnvironmentPtr eep_dp,
unsigned int symbol,
BiContextTypePtr ctx);
static void unary_exp_golomb_mv_encode(EncodingEnvironmentPtr eep_dp,
unsigned int symbol,
BiContextTypePtr ctx,
unsigned int max_bin);
/*!
************************************************************************
* \brief
* Check for available neighbouring blocks
* and set pointers in current macroblock
************************************************************************
*/
void CheckAvailabilityOfNeighborsCABAC(Macroblock *currMB)
{
PixelPos up, left;
int *mb_size = img->mb_size[IS_LUMA];
getNeighbour(currMB, -1, 0, mb_size, &left);
getNeighbour(currMB, 0, -1, mb_size, &up);
if (up.available)
currMB->mb_available_up = &img->mb_data[up.mb_addr];
else
currMB->mb_available_up = NULL;
if (left.available)
currMB->mb_available_left = &img->mb_data[left.mb_addr];
else
currMB->mb_available_left = NULL;
}
/*!
************************************************************************
* \brief
* Allocation of contexts models for the motion info
* used for arithmetic encoding
************************************************************************
*/
MotionInfoContexts* create_contexts_MotionInfo(void)
{
MotionInfoContexts* enco_ctx;
enco_ctx = (MotionInfoContexts*) calloc(1, sizeof(MotionInfoContexts) );
if( enco_ctx == NULL )
no_mem_exit("create_contexts_MotionInfo: enco_ctx");
return enco_ctx;
}
/*!
************************************************************************
* \brief
* Allocates of contexts models for the texture info
* used for arithmetic encoding
************************************************************************
*/
TextureInfoContexts* create_contexts_TextureInfo(void)
{
TextureInfoContexts* enco_ctx = (TextureInfoContexts*) calloc(1, sizeof(TextureInfoContexts) );
if( enco_ctx == NULL )
no_mem_exit("create_contexts_TextureInfo: enco_ctx");
return enco_ctx;
}
/*!
************************************************************************
* \brief
* Frees the memory of the contexts models
* used for arithmetic encoding of the motion info.
************************************************************************
*/
void delete_contexts_MotionInfo(MotionInfoContexts *enco_ctx)
{
if( enco_ctx == NULL )
return;
free( enco_ctx );
}
/*!
************************************************************************
* \brief
* Frees the memory of the contexts models
* used for arithmetic encoding of the texture info.
************************************************************************
*/
void delete_contexts_TextureInfo(TextureInfoContexts *enco_ctx)
{
if( enco_ctx == NULL )
return;
free( enco_ctx );
}
/*!
***************************************************************************
* \brief
* This function is used to arithmetically encode the field
* mode info of a given MB in the case of mb-based frame/field decision
***************************************************************************
*/
void writeFieldModeInfo_CABAC(SyntaxElement *se, DataPartition *dp)
{
EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
int curr_len = arienco_bits_written(eep_dp);
Macroblock *currMB = &img->mb_data[img->current_mb_nr];
int a = currMB->mbAvailA ? img->mb_data[currMB->mbAddrA].mb_field : 0;
int b = currMB->mbAvailB ? img->mb_data[currMB->mbAddrB].mb_field : 0;
int act_ctx = a + b;
#if ENABLE_FIELD_CTX
MotionInfoContexts *ctx = (img->currentSlice)->mot_ctx;
int mb_field = se->value1;
biari_encode_symbol(eep_dp, (signed short) (mb_field != 0), &ctx->mb_aff_contexts[act_ctx]);
#endif
se->context = act_ctx;
dp->bitstream->write_flag = 1;
se->len = (arienco_bits_written(eep_dp) - curr_len);
CABAC_TRACE;
}
/*!
***************************************************************************
* \brief
* This function is used to arithmetically encode the mb_skip_flag.
***************************************************************************
*/
void writeMB_skip_flagInfo_CABAC(Macroblock *currMB, SyntaxElement *se, DataPartition *dp)
{
EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
int curr_len = arienco_bits_written(eep_dp);
MotionInfoContexts *ctx = (img->currentSlice)->mot_ctx;
int curr_mb_type = se->value1;
int a = (currMB->mb_available_left != NULL && (currMB->mb_available_left->skip_flag == 0)) ? 1 : 0;
int b = (currMB->mb_available_up != NULL && (currMB->mb_available_up ->skip_flag == 0)) ? 1 : 0;
int act_ctx = a + b;
if (img->type == B_SLICE)
{
act_ctx += 7;
if (se->value1==0 && se->value2==0) // DIRECT mode, no coefficients
biari_encode_symbol (eep_dp, 1, &ctx->mb_type_contexts[2][act_ctx]);
else
biari_encode_symbol (eep_dp, 0, &ctx->mb_type_contexts[2][act_ctx]);
currMB->skip_flag = (se->value1==0 && se->value2==0) ? 1 : 0;
}
else
{
if (curr_mb_type==0) // SKIP
biari_encode_symbol(eep_dp, 1,&ctx->mb_type_contexts[1][act_ctx]);
else
biari_encode_symbol(eep_dp, 0,&ctx->mb_type_contexts[1][act_ctx]);
currMB->skip_flag = (curr_mb_type==0) ? 1 : 0;
}
se->context = act_ctx;
se->value1 = 1 - currMB->skip_flag;
dp->bitstream->write_flag = 1;
se->len = (arienco_bits_written(eep_dp) - curr_len);
CABAC_TRACE;
}
/*!
***************************************************************************
* \brief
* This function is used to arithmetically encode the macroblock
* intra_pred_size flag info of a given MB.
***************************************************************************
*/
void writeMB_transform_size_CABAC(SyntaxElement *se, DataPartition *dp)
{
EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
MotionInfoContexts *ctx = (img->currentSlice)->mot_ctx;
Macroblock *currMB = &img->mb_data[img->current_mb_nr];
int curr_len = arienco_bits_written(eep_dp);
int act_sym = currMB->luma_transform_size_8x8_flag;
int b = (currMB->mb_available_up == NULL) ? 0 : currMB->mb_available_up->luma_transform_size_8x8_flag;
int a = (currMB->mb_available_left == NULL) ? 0 :currMB->mb_available_left->luma_transform_size_8x8_flag;
int act_ctx = a + b;
se->context = act_ctx; // store context
biari_encode_symbol(eep_dp, (signed short) (act_sym != 0), ctx->transform_size_contexts + act_ctx );
dp->bitstream->write_flag = 1;
se->len = (arienco_bits_written(eep_dp) - curr_len);
CABAC_TRACE;
}
/*!
***************************************************************************
* \brief
* This function is used to arithmetically encode the macroblock
* type info of a given MB.
***************************************************************************
*/
void writeMB_typeInfo_CABAC(SyntaxElement *se, DataPartition *dp)
{
EncodingEnvironmentPtr eep_dp = &(dp->ee_cabac);
int curr_len = arienco_bits_written(eep_dp);
int a, b;
int act_ctx = 0;
int act_sym;
signed short csym;
int bframe = (img->type==B_SLICE);
int mode_sym = 0;
int mode16x16;
MotionInfoContexts *ctx = (img->currentSlice)->mot_ctx;
Macroblock *currMB = &img->mb_data[img->current_mb_nr];
int curr_mb_type = se->value1;
if(img->type == I_SLICE) // INTRA-frame
{
if (currMB->mb_available_up == NULL)
b = 0;
else
b = ((currMB->mb_available_up->mb_type != I4MB && currMB->mb_available_up->mb_type != I8MB) ? 1 : 0 );
if (currMB->mb_available_left == NULL)
a = 0;
else
a = ((currMB->mb_available_left->mb_type != I4MB && currMB->mb_available_left->mb_type != I8MB) ? 1 : 0 );
act_ctx = a + b;
act_sym = curr_mb_type;
se->context = act_ctx; // store context
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -