//++ 以下是对当前宏块进行编码(这里的编码主要目的是为了计算按当前组合方式编码
//++ 生成码流的大小,因此编码前要保存当前编码状态,编码完成后必须恢复现场)
//===== S T O R E C O D I N G S T A T E =====
//---------------------------------------------------
store_coding_state (cs_cm); //++ 保存当前编码状态
经常会看到store_coding_state所以有的时候很不明白到底为什么要保存当前编码状态。
今天看到firstime老大的注释,我也些许明白了点。因为在编码前要保存当前编码状态,这样编码完成后才能恢复现场。
看下代码吧
* /brief
* store coding state (for rd-optimized mode decision)
void
store_coding_state (CSptr cs)
{
int i;
EncodingEnvironment *ee_src, *ee_dest;
Bitstream *bs_src, *bs_dest;
MotionInfoContexts *mc_src = img->currentSlice->mot_ctx;
TextureInfoContexts *tc_src = img->currentSlice->tex_ctx;
MotionInfoContexts *mc_dest = cs->mot_ctx;
TextureInfoContexts *tc_dest = cs->tex_ctx;
Macroblock *currMB = &(img->mb_data [img->current_mb_nr]);
if (!input->rdopt) return;//如果不是RDO,则返回
if (cs->symbol_mode==CABAC)
{
//=== important variables of data partition array ===重要的数据内容数组的一些变量
//only one partition for IDR img对于IDR图像只有一个分区
for (i = 0; i <(img->currentPicture->idr_flag? 1:cs->no_part); i++)//如果是IDR则只执行一次,其他的执行no_part次
{
ee_src = &(img->currentSlice->partArr[i].ee_cabac);
bs_src = img->currentSlice->partArr[i].bitstream;
ee_dest = &(cs->encenv [i]);
bs_dest = &(cs->bitstream[i]);
memcpy (ee_dest, ee_src, sizeof(EncodingEnvironment));
memcpy (bs_dest, bs_src, sizeof(Bitstream));
}
//=== contexts for binary arithmetic coding ===储存算术编码的上下文
memcpy (mc_dest, mc_src, sizeof(MotionInfoContexts));
memcpy (tc_dest, tc_src, sizeof(TextureInfoContexts));
}
else
{
//=== important variables of data partition array ===在数据块重要的变量
for (i = 0; i <(img->currentPicture->idr_flag? 1:cs->no_part); i++)
{
bs_src = img->currentSlice->partArr[i].bitstream;
bs_dest = &(cs->bitstream[i]);
memcpy (bs_dest, bs_src, sizeof(Bitstream));
}
}
//=== syntax element number and bitcounters ===语法元素的成员和位数
cs->currSEnr = currMB->currSEnr;
memcpy (cs->bitcounter, currMB->bitcounter, MAX_BITCOUNTER_MB*sizeof(int));
//=== elements of current macroblock ===储存元素和现在的宏块
memcpy (cs->mvd, currMB->mvd, 2*2*BLOCK_MULTIPLE*BLOCK_MULTIPLE*sizeof(int));
cs->cbp_bits = currMB->cbp_bits;