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

📄 macroblock.c

📁 JM 11.0 KTA 2.1 Source Code
💻 C
📖 第 1 页 / 共 5 页
字号:
    if (!new_slice)
    {
      if(slice_too_big(rlc_bits))
      {
        *recode_macroblock = TRUE;
        *end_of_slice = TRUE;
      }
      else if(!img->cod_counter)
        skip = FALSE;
    }
    // maximum number of MBs
    
    // check if current slice group is finished
    if ((*recode_macroblock == FALSE) && (img->current_mb_nr == FmoGetLastCodedMBOfSliceGroup (FmoMB2SliceGroup (img->current_mb_nr)))) 
    {
      *end_of_slice = TRUE;
      if(!img->cod_counter)
        skip = FALSE;
    }
    
    //! (first MB OR first MB in a slice) AND bigger that maximum size of slice
    if (new_slice && slice_too_big(rlc_bits))
    {
      *end_of_slice = TRUE;
      if(!img->cod_counter)
        skip = FALSE;
    }
    if (!*recode_macroblock)
      currSlice->num_mb++;
    break;
    
  case  CALLBACK:
    if (img->current_mb_nr > 0 && !new_slice)
    {
      if (currSlice->slice_too_big(rlc_bits))
      {
        *recode_macroblock = TRUE;
        *end_of_slice = TRUE;
      }
    }
    
    if ( (*recode_macroblock == FALSE) && (img->current_mb_nr == FmoGetLastCodedMBOfSliceGroup (FmoMB2SliceGroup (img->current_mb_nr)))) 
      *end_of_slice = TRUE;
    break;
    
  default:
    snprintf(errortext, ET_SIZE, "Slice Mode %d not supported", input->slice_mode);
    error(errortext, 600);
  }
  
  if(*recode_macroblock == TRUE)
  {
    // Restore everything
    for (i=0; i<currSlice->max_part_nr; i++)
    {
      dataPart = &(currSlice->partArr[i]);
      currStream = dataPart->bitstream;
      currStream->bits_to_go = currStream->stored_bits_to_go;
      currStream->byte_pos  = currStream->stored_byte_pos;
      currStream->byte_buf  = currStream->stored_byte_buf;
      if (input->symbol_mode == CABAC)
      {
        eep = &(dataPart->ee_cabac);
        eep->Elow            = eep->ElowS;
        eep->Erange          = eep->ErangeS;
        eep->Ebuffer         = eep->EbufferS;
        eep->Ebits_to_go     = eep->Ebits_to_goS;
        eep->Ebits_to_follow = eep->Ebits_to_followS;
        eep->Ecodestrm       = eep->EcodestrmS;
        eep->Ecodestrm_len   = eep->Ecodestrm_lenS;
        eep->C               = eep->CS;
        eep->E               = eep->ES;       
      }
    }
  }
  
  if (input->symbol_mode == UVLC)
  {
    // Skip MBs at the end of this slice
    dataPart = &(currSlice->partArr[partMap[SE_MBTYPE]]);
    if(*end_of_slice == TRUE  && skip == TRUE) 
    { 
      // only for Slice Mode 2 or 3
      // If we still have to write the skip, let's do it!
      if(img->cod_counter && *recode_macroblock == TRUE) // MB that did not fit in this slice
      { 
        // If recoding is true and we have had skip, 
        // we have to reduce the counter in case of recoding
        img->cod_counter--;
        if(img->cod_counter)
        {
          currSE->value1 = img->cod_counter;
          currSE->value2 = 0;
          currSE->mapping = ue_linfo;
          currSE->type = SE_MBTYPE;
          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;
        }
      }
      else //! MB that did not fit in this slice anymore is not a Skip MB
      {
        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;
      }
    }
    
    // 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->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;
}

static pel_t *(*get_line) (pel_t**, int, int, int, int);
/*!
************************************************************************
* \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 //!< reference picture list
                                    )
{
  pel_t** ref_pic = list[ref]->imgY_ups;
  int img_width =((list[ref]->size_x + 2*IMG_PAD_SIZE - 1)<<2);
  int img_height=((list[ref]->size_y + 2*IMG_PAD_SIZE - 1)<<2);
  pel_t   *ref_line;
  int j;
  int i0,j0;
  
  int mv0, mv1;
  mv0 = mv[0];
  mv1 = mv[1];
  
  
#ifdef MV_COMPETITION
  if ((skip_mode== TRUE) &&  (input->mv_competition > 0))
  {
    mv0 = mv_comp.mv_pred_skip[mv_comp.current_predictor_for_skip][0];
    mv1 = mv_comp.mv_pred_skip[mv_comp.current_predictor_for_skip][1];
  }
#endif
  
#ifdef EIGHTH_PEL     
  if (img->mv_res)
  {
    j0      = (pic_pix_y << 3) + mv1 + IMG_PAD_SIZE_TIMES4*2;
    i0      = (pic_pix_x << 3) + mv0 + IMG_PAD_SIZE_TIMES4*2;
  }
  else
  {
    j0      = (pic_pix_y << 2) + mv1 + IMG_PAD_SIZE_TIMES4;
    i0      = (pic_pix_x << 2) + mv0 + IMG_PAD_SIZE_TIMES4;
  }
#else//EIGHTH_PEL
  j0      = (pic_pix_y << 2) + mv1 + IMG_PAD_SIZE_TIMES4;
  i0      = (pic_pix_x << 2) + mv0 + IMG_PAD_SIZE_TIMES4;
#endif
  
  
#ifdef EIGHTH_PEL
  if (img->mv_res)
  {
    if ((i0 > 0) && (i0 < (((img_width - 4*BLOCK_SIZE)<< 1) - 1))  && (j0 > 0) && (j0 < (((img_height - 4*BLOCK_SIZE)<< 1) - 1)))
      get_line = FastLine4X8;
    else
      get_line = UMVLine4X8;
  }
  else
  {
    if ((i0 > 0) && (i0 < img_width - 4*BLOCK_SIZE - 1) && (j0 > 0) && (j0 < img_height - 4*BLOCK_SIZE - 1))
      get_line = FastLine4X;
    else
      get_line = UMVLine4X;
  }
#else//EIGHTH_PEL
  if ((i0 > 0) && (i0 < img_width - 4*BLOCK_SIZE - 1) && (j0 > 0) && (j0 < img_height - 4*BLOCK_SIZE - 1))
    get_line = FastLine4X;
  else
    get_line = UMVLine4X;
  
#endif
  
  
#ifdef EIGHTH_PEL
  for (j = j0; j < j0 + (MB_BLOCK_SIZE<<img->mv_res); j+=BLOCK_SIZE<<img->mv_res)
#else
    for (j = j0; j < j0 + (MB_BLOCK_SIZE); j+=BLOCK_SIZE)
#endif
    {
      ref_line = get_line (ref_pic, j, i0, img_height, img_width);
      *mpred++ = *ref_line;
      *mpred++ = *(ref_line + 4);
      *mpred++ = *(ref_line + 8);
      *mpred++ = *(ref_line + 12);
    }
}

/*!
************************************************************************
* \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] //!< target block
                   )        
{
  *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=list0, 1=list1, 2=bipred)
                        int   fw_mode,    //!< list0 prediction mode (1-7, 0=DIRECT if bw_mode=0)
                        int   bw_mode,    //!< list1 prediction mode (1-7, 0=DIRECT if fw_mode=0)
                        short fw_ref_idx, //!< reference frame for list0 prediction (-1: Intra4x4 pred. with fw_mode)
                        short bw_ref_idx  //!< reference frame for list1 prediction 
                        )
{
  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[by][bx];
#ifdef  USE_HP_FILTER//BROUND
  img->bipred_rounding_control = (img->nal_reference_idc!=0);
#endif
  
  
  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[by][bx] : img->bipred_mv2[by][bx];
  }
  
  if ((p_dir==0)||(p_dir==2))
  {
    OneComponentLumaPrediction4x4 (fw_pred, pic_opix_x, pic_opix_y, mv_array[LIST_0][fw_ref_idx][fw_mode], fw_ref_idx, listX[0+currMB->list_offset]);   
  }
  
  if ((p_dir==1)||(p_dir==2))
  { 
    OneComponentLumaPrediction4x4 (bw_pred, pic_opix_x, pic_opix_y, mv_array[LIST_1][bw_ref_idx][bw_mode], bw_ref_idx, listX[1+currMB->list_offset]);   
  }
  
  if (apply_weights)
  {
    
    if (p_dir==2)
    {

⌨️ 快捷键说明

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