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

📄 macroblock.c

📁 H.264的JM源码93a版本
💻 C
📖 第 1 页 / 共 5 页
字号:
        currSE->type = SE_MBTYPE;
        dataPart = &(currSlice->partArr[partMap[currSE->type]]);
        dataPart->writeSyntaxElement(  currSE, dataPart);
        rlc_bits=currSE->len;
        currMB->bitcounter[BITS_MB_MODE]+=rlc_bits;
        img->cod_counter = 0;
      }
    }
    else //! MB that did not fit in this slice anymore is not a Skip MB
    {
      dataPart = &(currSlice->partArr[partMap[SE_MBTYPE]]);       
      currStream = dataPart->bitstream;
        // update the bitstream
      currStream->bits_to_go = currStream->bits_to_go_skip;
      currStream->byte_pos  = currStream->byte_pos_skip;
      currStream->byte_buf  = currStream->byte_buf_skip;

      // update the statistics
      img->cod_counter = 0;
      skip = FALSE;
    }
  }
  
  //! TO 4.11.2001 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)
  {
    currSE->value1 = img->cod_counter;
    currSE->value2 = 0;
    currSE->mapping = ue_linfo;
    currSE->type = SE_MBTYPE;
    dataPart = &(currSlice->partArr[partMap[currSE->type]]);
    dataPart->writeSyntaxElement(  currSE, dataPart);
     currMB->currSEnr ++;
#if TRACE
    snprintf(currSE->tracestring, TRACESTRING_SIZE, "Final MB runlength = %3d",img->cod_counter); 
#endif
   
    rlc_bits=currSE->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;
  
   //! UVLC
   if (input->symbol_mode == UVLC)
   {
     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 one component of a 4x4 Luma block
 ************************************************************************
 */
void
OneComponentLumaPrediction4x4 (int*   mpred,      //  --> array of prediction values (row by row)
                               int    pic_pix_x,  // <--  absolute horizontal coordinate of 4x4 block
                               int    pic_pix_y,  // <--  absolute vertical   coordinate of 4x4 block
                               short* mv,         // <--  motion vector
                               short  ref,        // <--  reference frame 
                               StorablePicture **list)
{
  pel_t** ref_pic;
  int     pix_add = 4;
  int     j0      = (pic_pix_y << 2) + mv[1], j1=j0+pix_add, j2=j1+pix_add, j3=j2+pix_add;
  int     i0      = (pic_pix_x << 2) + mv[0], i1=i0+pix_add, i2=i1+pix_add, i3=i2+pix_add;
  
  pel_t (*get_pel) (pel_t**, int, int, int, int) = UMVPelY_14;

  int img_width =list[ref]->size_x;
  int img_height=list[ref]->size_y;

  ref_pic   = list[ref]->imgY_ups;
  
  *mpred++ = get_pel (ref_pic, j0, i0, img_height, img_width);
  *mpred++ = get_pel (ref_pic, j0, i1, img_height, img_width);
  *mpred++ = get_pel (ref_pic, j0, i2, img_height, img_width);
  *mpred++ = get_pel (ref_pic, j0, i3, img_height, img_width);
  *mpred++ = get_pel (ref_pic, j1, i0, img_height, img_width);
  *mpred++ = get_pel (ref_pic, j1, i1, img_height, img_width);
  *mpred++ = get_pel (ref_pic, j1, i2, img_height, img_width);
  *mpred++ = get_pel (ref_pic, j1, i3, img_height, img_width);
  *mpred++ = get_pel (ref_pic, j2, i0, img_height, img_width);
  *mpred++ = get_pel (ref_pic, j2, i1, img_height, img_width);
  *mpred++ = get_pel (ref_pic, j2, i2, img_height, img_width);
  *mpred++ = get_pel (ref_pic, j2, i3, img_height, img_width);
  *mpred++ = get_pel (ref_pic, j3, i0, img_height, img_width);
  *mpred++ = get_pel (ref_pic, j3, i1, img_height, img_width);
  *mpred++ = get_pel (ref_pic, j3, i2, img_height, img_width);
  *mpred++ = get_pel (ref_pic, j3, i3, img_height, img_width);

}

/*!
 ************************************************************************
 * \brief
 *    copy foward/backward prediction values of one component of a 4x4 Luma block
 ************************************************************************
 */

void
copyblock4x4 (int*   mpred,      //  --> array of prediction values (row by row)
              int block[BLOCK_SIZE][BLOCK_SIZE])        
{
  *mpred++ = block[0][0];
  *mpred++ = block[1][0];
  *mpred++ = block[2][0];
  *mpred++ = block[3][0];
  *mpred++ = block[0][1];
  *mpred++ = block[1][1];
  *mpred++ = block[2][1];
  *mpred++ = block[3][1];
  *mpred++ = block[0][2];
  *mpred++ = block[1][2];
  *mpred++ = block[2][2];
  *mpred++ = block[3][2];
  *mpred++ = block[0][3];
  *mpred++ = block[1][3];
  *mpred++ = block[2][3];
  *mpred++ = block[3][3];
}

/*!
 ************************************************************************
 * \brief
 *    Predict one 4x4 Luma block
 ************************************************************************
 */
void
LumaPrediction4x4 (int   block_x,    // <--  relative horizontal block coordinate of 4x4 block
                   int   block_y,    // <--  relative vertical   block coordinate of 4x4 block
                   int   p_dir,      // <--  prediction direction (0=forward, 1=backward, 2=bidir)
                   int   fw_mode,    // <--  forward  prediction mode (1-7, 0=DIRECT if bw_mode=0)
                   int   bw_mode,    // <--  backward prediction mode (1-7, 0=DIRECT if fw_mode=0)
                   short fw_ref_idx, // <--  reference frame for forward prediction (-1: Intra4x4 pred. with fw_mode)
                   short bw_ref_idx  )    
{
  static int fw_pred[16];
  static int bw_pred[16];

  int  i, j;
  int  block_x4  = block_x+4;
  int  block_y4  = block_y+4;
  int  pic_opix_x = img->opix_x + block_x;
  int  pic_opix_y = img->opix_y + block_y;
  int  bx        = block_x >> 2;
  int  by        = block_y >> 2;
  int* fpred     = fw_pred;
  int* bpred     = bw_pred;
  Macroblock*    currMB     = &img->mb_data[img->current_mb_nr];

  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 = img->all_mv;

  int  list_offset   = ((img->MbaffFrameFlag)&&(currMB->mb_field))? img->current_mb_nr%2 ? 4 : 2 : 0;

#if BI_PREDICTION
  if (currMB->bi_pred_me && fw_ref_idx == 0 && bw_ref_idx == 0 && p_dir == 2 && fw_mode==1 && bw_mode==1)
  {
    mv_array = currMB->bi_pred_me == 1? img->bipred_mv1 : img->bipred_mv2 ;
  }
#endif

  if ((p_dir==0)||(p_dir==2))
  {
    OneComponentLumaPrediction4x4 (fw_pred, pic_opix_x, pic_opix_y, mv_array[bx][by][LIST_0][fw_ref_idx][fw_mode], fw_ref_idx, listX[0+list_offset]);   
  }

  if ((p_dir==1)||(p_dir==2))
  { 
    OneComponentLumaPrediction4x4 (bw_pred, pic_opix_x, pic_opix_y, mv_array[bx][by][LIST_1][bw_ref_idx][bw_mode], bw_ref_idx, listX[1+list_offset]);   
  }

  if (apply_weights)
  {

    if (p_dir==2)
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  
          img->mpr[i][j] = clip1a(((wbp_weight[0][fw_ref_idx][bw_ref_idx][0] * *fpred++ + 
                                    wbp_weight[1][fw_ref_idx][bw_ref_idx][0] * *bpred++ + 
                                    2*wp_luma_round) >> (luma_log_weight_denom + 1)) + 
                                    ((wp_offset[0][fw_ref_idx][0] + wp_offset[1][bw_ref_idx][0] + 1)>>1)); 
    }
    else if (p_dir==0)
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  
          img->mpr[i][j] = clip1a(((wp_weight[0][fw_ref_idx][0] * *fpred++  + wp_luma_round) >> luma_log_weight_denom) +
                                    wp_offset[0][fw_ref_idx][0] );
    }
    else // p_dir==1
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  
          img->mpr[i][j] = clip1a(((wp_weight[1][bw_ref_idx][0] * *bpred++  + wp_luma_round) >> luma_log_weight_denom) +
                                    wp_offset[1][bw_ref_idx][0] );
    }


  }
  else
  {
    if (p_dir==2)
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  
          img->mpr[i][j] = (*fpred++ + *bpred++ + 1) / 2; 
    }
    else if (p_dir==0)
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  img->mpr[i][j] = *fpred++;
    }
    else // p_dir==1
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  img->mpr[i][j] = *bpred++;
    }
  }
}
#if BI_PREDICTION
/*!
************************************************************************
* \brief
*    Predict one 4x4 Luma block
************************************************************************
*/
void
LumaPrediction4x4Bi (int  block_x,    // <--  relative horizontal block coordinate of 4x4 block
                   int  block_y,    // <--  relative vertical   block coordinate of 4x4 block
                   int  p_dir,      // <--  prediction direction (0=forward, 1=backward, 2=bidir)
                   int  fw_mode,    // <--  forward  prediction mode (1-7, 0=DIRECT if bw_mode=0)
                   int  bw_mode,    // <--  backward prediction mode (1-7, 0=DIRECT if fw_mode=0)
                   short  fw_ref_idx, // <--  reference frame for forward prediction (-1: Intra4x4 pred. with fw_mode)
                   short  bw_ref_idx,
                   int  list)    
{
  static int fw_pred[16];
  static int bw_pred[16];

  int  i, j;
  int  block_x4  = block_x+4;
  int  block_y4  = block_y+4;
  int  pic_opix_x = img->opix_x + block_x;
  int  pic_opix_y = img->opix_y + block_y;
  int  bx        = block_x >> 2;
  int  by        = block_y >> 2;
  int* fpred     = fw_pred;
  int* bpred     = bw_pred;
  Macroblock*    currMB     = &img->mb_data[img->current_mb_nr];

   
  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)));  

  int  list_offset   = ((img->MbaffFrameFlag)&&(currMB->mb_field))? img->current_mb_nr%2 ? 4 : 2 : 0;

  short   ******bipred_mv = list ? img->bipred_mv1 : img->bipred_mv2;

  if ((p_dir==0)||(p_dir==2))
  {
      //OneComponentLumaPrediction4x4 (fw_pred, pic_opix_x, pic_opix_y, bipred_mv[bx][by][LIST_0][fw_ref_idx][fw_mode], fw_ref_idx, listX[0+list_offset]);   
      OneComponentLumaPrediction4x4 (fw_pred, pic_opix_x, pic_opix_y, bipred_mv[bx][by][LIST_0][fw_ref_idx][fw_mode], fw_ref_idx, listX[0+list_offset]);   
  }

  if ((p_dir==1)||(p_dir==2))
  { 
      OneComponentLumaPrediction4x4 (bw_pred, pic_opix_x, pic_opix_y, bipred_mv[bx][by][LIST_1][bw_ref_idx][bw_mode], bw_ref_idx, listX[1+list_offset]);   
  }

  if (apply_weights)
  {
    if (p_dir==2)
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  
          img->mpr[i][j] = clip1a(((wbp_weight[0][fw_ref_idx][bw_ref_idx][0] * *fpred++ + 
                                    wbp_weight[1][fw_ref_idx][bw_ref_idx][0] * *bpred++ + 
                                    2*wp_luma_round) >> (luma_log_weight_denom + 1)) + 
                                    ((wp_offset[0][fw_ref_idx][0] + wp_offset[1][bw_ref_idx][0] + 1)>>1)); 
    }
    else if (p_dir==0)
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)                   
          img->mpr[i][j] = clip1a(((wp_weight[0][fw_ref_idx][0] * *fpred++  + wp_luma_round) >> luma_log_weight_denom) +
          + wp_offset[0][fw_ref_idx][0] );        
    }
    else // p_dir==1
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  
          img->mpr[i][j] = clip1a(((wp_weight[1][bw_ref_idx][0] * *bpred++  + wp_luma_round) >> luma_log_weight_denom) +
          wp_offset[1][bw_ref_idx][0] );
    }
  }
  else
  {
    if (p_dir==2)
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  
          img->mpr[i][j] = (*fpred++ + *bpred++ + 1) / 2; 
    }
    else if (p_dir==0)
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  img->mpr[i][j] = *fpred++;
    }
    else // p_dir==1
    {
      for   (j=block_y; j<block_y4; j++)
        for (i=block_x; i<block_x4; i++)  img->mpr[i][j] = *bpred++;
    }
  }
}
#endif


/*!
 ************************************************************************
 * \brief
 *    Residual Coding of an 8x8 Luma block (not for intra)
 ************************************************************************

⌨️ 快捷键说明

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