⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 macroblock.c

📁 h.264 影像壓縮 必須在 .net 的環境 下操作
💻 C
📖 第 1 页 / 共 5 页
字号:
        // update the statistics
        img->cod_counter = 0;
        skip = FALSE;
      }
    }

    // Skip MBs at the end of this slice for Slice Mode 0 or 1
    if(*end_of_slice == TRUE && img->cod_counter && !use_bitstream_backing)
    {
      se.value1 = img->cod_counter;
      se.value2 = 0;
      se.type = SE_MBTYPE;

      TRACE_SE (se.tracestring, "mb_skip_run");
      writeSE_UVLC(&se, dataPart);

      rlc_bits=se.len;
      currMB->bitcounter[BITS_MB_MODE]+=rlc_bits;
      img->cod_counter = 0;
    }
  }
}

/*!
 *****************************************************************************
 *
 * \brief
 *    For Slice Mode 2: Checks if one partition of one slice exceeds the
 *    allowed size
 *
 * \return
 *    FALSE if all Partitions of this slice are smaller than the allowed size
 *    TRUE is at least one Partition exceeds the limit
 *
 * \par Side effects
 *    none
 *
 * \date
 *    4 November 2001
 *
 * \author
 *    Tobias Oelbaum      drehvial@gmx.net
 *****************************************************************************/

int slice_too_big(int rlc_bits)
{
  Slice *currSlice = img->currentSlice;
  DataPartition *dataPart;
  Bitstream *currStream;
  EncodingEnvironmentPtr eep;
  int i;
  int size_in_bytes;

  //! CAVLC
  if (input->symbol_mode == CAVLC)
  {
    for (i=0; i<currSlice->max_part_nr; i++)
    {
      dataPart = &(currSlice->partArr[i]);
      currStream = dataPart->bitstream;
      size_in_bytes = currStream->byte_pos /*- currStream->tmp_byte_pos*/;

      if (currStream->bits_to_go < 8)
        size_in_bytes++;
      if (currStream->bits_to_go < rlc_bits)
        size_in_bytes++;
      if(size_in_bytes > input->slice_argument)
        return TRUE;
    }
  }

  //! CABAC
  if (input->symbol_mode ==CABAC)
  {
    for (i=0; i<currSlice->max_part_nr; i++)
    {
      dataPart= &(currSlice->partArr[i]);
      eep = &(dataPart->ee_cabac);

      if( arienco_bits_written(eep) > (input->slice_argument*8))
        return TRUE;
    }
  }
  return FALSE;
}


/*!
 ************************************************************************
 * \brief
 *    Predict Luma block
 ************************************************************************
 */
void OneComponentLumaPrediction ( imgpel*   mpred,       //!< array of prediction values (row by row)
                                  int    pic_pix_x,      //!< motion shifted horizontal coordinate of block
                                  int    pic_pix_y,      //!< motion shifted vertical   coordinate of block
                                  int    block_size_x,   //!< horizontal block size
                                  int    block_size_y,   //!< vertical block size
                                  StorablePicture *list //!< reference picture list
                                 )
{
  int     j;
  imgpel *ref_line = UMVLine4X (list->p_curr_img_sub, pic_pix_y, pic_pix_x);

  width_pad  = list->size_x_pad;
  height_pad = list->size_y_pad;
  
  for (j = 0; j < block_size_y; j++) 
  {
    memcpy(mpred, ref_line, block_size_x * sizeof(imgpel));
    ref_line += img_padded_size_x;
    mpred += block_size_x;
  }  
}


/*!
 ************************************************************************
 * \brief
 *    Predict one Luma block
 ************************************************************************
 */
void LumaPrediction ( Macroblock* currMB,//!< Current Macroblock
                     int   block_x,     //!< relative horizontal block coordinate of block
                     int   block_y,     //!< relative vertical   block coordinate of block
                     int   block_size_x,//!< relative horizontal block coordinate of block
                     int   block_size_y,//!< relative vertical   block coordinate of block
                     int   p_dir,       //!< prediction direction (0=list0, 1=list1, 2=bipred)
                     int   l0_mode,     //!< list0 prediction mode (1-7, 0=DIRECT if l1_mode=0)
                     int   l1_mode,     //!< list1 prediction mode (1-7, 0=DIRECT if l0_mode=0)
                     short l0_ref_idx,  //!< reference frame for list0 prediction (-1: Intra4x4 pred. with l0_mode)
                     short l1_ref_idx   //!< reference frame for list1 prediction 
                     )
{
  int  i, j;
  int  block_x4     = block_x + block_size_x;
  int  block_y4     = block_y + block_size_y;
  int  pic_opix_x   = ((img->opix_x + block_x) << 2) + IMG_PAD_SIZE_TIMES4;
  int  pic_opix_y   = ((img->opix_y + block_y) << 2) + IMG_PAD_SIZE_TIMES4;
  int  bx           = block_x >> 2;
  int  by           = block_y >> 2;
  imgpel* l0pred    = l0_pred;
  imgpel* l1pred    = l1_pred;  
  short**** mv_array = img->all_mv[by][bx];
  imgpel (*curr_mpr)[16] = img->mpr[0];

  int  apply_weights = ( (active_pps->weighted_pred_flag  && (img->type== P_SLICE || img->type == SP_SLICE)) ||
    (active_pps->weighted_bipred_idc && (img->type== B_SLICE)));

  if (currMB->bi_pred_me && l0_ref_idx == 0 && l1_ref_idx == 0 && p_dir == 2 && l0_mode==1 && l1_mode==1)
    mv_array = currMB->bi_pred_me == 1? img->bipred_mv1[by][bx] : img->bipred_mv2[by][bx];

  switch (p_dir)
  {
  case 0:
    OneComponentLumaPrediction (l0_pred, pic_opix_x + mv_array[LIST_0][l0_ref_idx][l0_mode][0], pic_opix_y + mv_array[LIST_0][l0_ref_idx][l0_mode][1], block_size_x, block_size_y, listX[0+currMB->list_offset][l0_ref_idx]);
    break;
  case 1:
    OneComponentLumaPrediction (l1_pred, pic_opix_x + mv_array[LIST_1][l1_ref_idx][l1_mode][0], pic_opix_y + mv_array[LIST_1][l1_ref_idx][l1_mode][1], block_size_x, block_size_y, listX[1+currMB->list_offset][l1_ref_idx]);
    break;
  case 2:
    OneComponentLumaPrediction (l0_pred, pic_opix_x + mv_array[LIST_0][l0_ref_idx][l0_mode][0], pic_opix_y + mv_array[LIST_0][l0_ref_idx][l0_mode][1], block_size_x, block_size_y, listX[0+currMB->list_offset][l0_ref_idx]);
    OneComponentLumaPrediction (l1_pred, pic_opix_x + mv_array[LIST_1][l1_ref_idx][l1_mode][0], pic_opix_y + mv_array[LIST_1][l1_ref_idx][l1_mode][1], block_size_x, block_size_y, listX[1+currMB->list_offset][l1_ref_idx]);
    break;
  default:
    break;
  }

  if (apply_weights)
  {
    if (p_dir==2)
    {
      int wbp0 = wbp_weight[0][l0_ref_idx][l1_ref_idx][0];
      int wbp1 = wbp_weight[1][l0_ref_idx][l1_ref_idx][0];
      int offset = (wp_offset[0][l0_ref_idx][0] + wp_offset[1][l1_ref_idx][0] + 1)>>1;
      int wp_round = 2*wp_luma_round;
      int weight_denom = luma_log_weight_denom + 1;
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  
          curr_mpr[j][i] = iClip1( img->max_imgpel_value, 
          ((wbp0 * *l0pred++ + wbp1 * *l1pred++ + wp_round) >> (weight_denom)) + offset); 
    }
    else if (p_dir==0)
    {
      int wp = wp_weight[0][l0_ref_idx][0];
      int offset = wp_offset[0][l0_ref_idx][0];
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)
          curr_mpr[j][i] = iClip1( img->max_imgpel_value, 
          ((wp * *l0pred++  + wp_luma_round) >> luma_log_weight_denom) + offset);
    }
    else // (p_dir==1)
    {
      int wp = wp_weight[1][l1_ref_idx][0];
      int offset = wp_offset[1][l1_ref_idx][0];
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)
          curr_mpr[j][i] = iClip1( img->max_imgpel_value, 
          ((wp * *l1pred++  + wp_luma_round) >> luma_log_weight_denom) + offset );
    }
  }
  else
  {
    if (p_dir==2)
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)
          curr_mpr[j][i] = (*l0pred++ + *l1pred++ + 1) >> 1;
    }
    else if (p_dir==0)
    {
      for (j=block_y; j<block_y4; j++)
      {
        memcpy(&(curr_mpr[j][block_x]), l0pred, block_size_x * sizeof(imgpel));
        l0pred += block_size_x;
      }
    }
    else // (p_dir==1)
    {
      for (j=block_y; j<block_y4; j++)
      {
        memcpy(&(curr_mpr[j][block_x]), l1pred, block_size_x * sizeof(imgpel));
        l1pred += block_size_x;
      }
    }
  }
}

/*!
 ************************************************************************
 * \brief
 *    Predict one Luma block
 ************************************************************************
 */
void LumaPredictionBi ( Macroblock* currMB, //!< Current Macroblock
                        int   block_x,      //!< relative horizontal block coordinate of 4x4 block
                        int   block_y,      //!< relative vertical   block coordinate of 4x4 block
                        int   block_size_x, //!< horizontal block size
                        int   block_size_y, //!< vertical   block size
                        int   l0_mode,      //!< list0 prediction mode (1-7, 0=DIRECT if l1_mode=0)
                        int   l1_mode,      //!< list1 prediction mode (1-7, 0=DIRECT if l0_mode=0)
                        short l0_ref_idx,   //!< reference frame for list0 prediction (-1: Intra4x4 pred. with l0_mode)
                        short l1_ref_idx,   //!< reference frame for list1 prediction 
                        int   list          //!< current list for prediction.
                        )
{
  int  i, j;
  int  block_x4  = block_x + block_size_x;
  int  block_y4  = block_y + block_size_y;
  int  pic_opix_x = ((img->opix_x + block_x) << 2) + IMG_PAD_SIZE_TIMES4;
  int  pic_opix_y = ((img->opix_y + block_y) << 2) + IMG_PAD_SIZE_TIMES4;
  int  bx        = block_x >> 2;
  int  by        = block_y >> 2;
  imgpel* l0pred     = l0_pred;
  imgpel* l1pred     = l1_pred;

  int  apply_weights = ( (active_pps->weighted_pred_flag && (img->type == P_SLICE || img->type == SP_SLICE)) ||
    (active_pps->weighted_bipred_idc && (img->type == B_SLICE)));  
  short   ****mv_array = list ? img->bipred_mv1[by][bx] : img->bipred_mv2[by][bx];
  imgpel (*curr_mpr)[16] = img->mpr[0];

  OneComponentLumaPrediction (l0_pred, pic_opix_x + mv_array[LIST_0][l0_ref_idx][l0_mode][0], pic_opix_y + mv_array[LIST_0][l0_ref_idx][l0_mode][1], block_size_x, block_size_y, listX[0+currMB->list_offset][l1_ref_idx]);
  OneComponentLumaPrediction (l1_pred, pic_opix_x + mv_array[LIST_1][l1_ref_idx][l1_mode][0], pic_opix_y + mv_array[LIST_1][l1_ref_idx][l1_mode][1], block_size_x, block_size_y, listX[1+currMB->list_offset][l1_ref_idx]);

  if (apply_weights)
  {
    int wbp0 = wbp_weight[0][l0_ref_idx][l1_ref_idx][0];
    int wbp1 = wbp_weight[1][l0_ref_idx][l1_ref_idx][0];
    int offset = (wp_offset[0][l0_ref_idx][0] + wp_offset[1][l1_ref_idx][0] + 1)>>1;
    int wp_round = 2*wp_luma_round;
    int weight_denom = luma_log_weight_denom + 1;

    for   (j=block_y; j<block_y4; j++)
      for (i=block_x; i<block_x4; i++)
        curr_mpr[j][i] = iClip1( img->max_imgpel_value,
        ((wbp0 * *l0pred++ + wbp1 * *l1pred++ + wp_round) >> weight_denom) + offset);
  }
  else
  {
    for   (j=block_y; j<block_y4; j++)
      for (i=block_x; i<block_x4; i++)
        curr_mpr[j][i] = (*l0pred++ + *l1pred++ + 1) >> 1;
  }
}


/*!
 ************************************************************************
 * \brief
 *    Residual Coding of an 8x8 Luma block (not for intra)
 *
 * \return
 *    coefficient cost
 ************************************************************************
 */
int LumaResidualCoding8x8 ( Macroblock* currMB, //!< Current Macroblock to be coded
                            int   *cbp,         //!< Output: cbp (updated according to processed 8x8 luminance block)
                            int64 *cbp_blk,     //!< Output: block cbp (updated according to processed 8x8 luminance block)
                            int   block8x8,     //!< block number of 8x8 block
                            short p_dir,        //!< prediction direction
                            int   l0_mode,      //!< list0 prediction mode (1-7, 0=DIRECT)
                            int   l1_mode,      //!< list1 prediction mode (1-7, 0=DIRECT)
                            short l0_ref_idx,   //!< reference picture for list0 prediction
                            short l1_ref_idx    //!< reference picture for list0 prediction
                           )
{
  int    block_y, block_x, pic_pix_y, pic_pix_x, i, j, nonzero = 0, cbp_blk_mask;
  int    coeff_cost = 0;
  int    mb_y       = (block8x8 >> 1) << 3;
  int    mb_x       = (block8x8 & 0x01) << 3;
  int    cbp_mask   = 1 << block8x8;
  int    bxx, byy;                   // indexing curr_blk
  int    skipped    = (l0_mode == 0 && l1_mode == 0 && (img->type != B_SLICE));
   
  //set transform size
  int    need_8x8_transform = currMB->luma_transform_size_8x8_flag;
  imgpel *imgOrg, *imgPred;
  int    *m7;
  imgpel (*curr_mpr)[16] = img->mpr[0];
  int    uv, nonzerocr[3]={0,0,0};  
  coeff_cost_cr[1] = coeff_cost_cr[2] = 0;

  //===== loop over 4x4 blocks =====
  if(!need_8x8_transform)
  {
    if (((p_dir == 0 || p_dir == 2 )&& l0_mode < 5) || ((p_dir == 1 || p_dir == 2 ) && l1_mode < 5))
    {
      LumaPrediction (currMB, mb_x, mb_y, 8, 8, p_dir, l0_mode, l1_mode, l0_ref_idx, l1_ref_idx);
      //===== get displaced frame difference ======
      for (j = mb_y; j < mb_y + 8; j++)
      {
        imgOrg = &pCurImg[img->opix_y + j][img->opix_x + mb_x];    
        imgPred = &curr_mpr[j][mb_x];
        m7 = &img->m7[0][j][mb_x]; 
        for (i = 0; i < 8; i++)
        {
          *m7++ = *imgOrg++ - *imgPred++;
        }
      }
    }

    for (byy=0, block_y=mb_y; block_y<mb_y+8; byy+=4, block_y+=4)
    {
      for (bxx=0, block_x=mb_x; block_x<mb_x+8; bxx+=4, block_x+=4)
      {
        pic_pix_x = img->opix_x + block_x;
        cbp_blk_mask = (block_x >> 2) + block_y;

        //===== prediction of 4x4 block =====
        if (!(((p_dir == 0 || p_dir == 2 )&& l0_mode < 5) || ((p_dir == 1 || p_dir == 2 ) && l1_mode < 5)))
        {
          LumaPrediction (currMB, block_x, block_y, 4, 4, p_dir, l0_mode, l1_mode, l0_ref_idx, l1_ref_idx);

          //===== get displaced frame difference ======
          for (j = block_y; j < block_y + 4; j++)
          {
            imgOrg = &pCurImg[img->opix_y + j][pic_pix_x];    
            imgPred = &curr_mpr[j][block_x];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -