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

📄 macroblock.c

📁 本源码是H.26L标准的Visual C++源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
/*
***********************************************************************
* COPYRIGHT AND WARRANTY INFORMATION
*
* Copyright 2001, International Telecommunications Union, Geneva
*
* DISCLAIMER OF WARRANTY
*
* These software programs are available to the user without any
* license fee or royalty on an "as is" basis. The ITU disclaims
* any and all warranties, whether express, implied, or
* statutory, including any implied warranties of merchantability
* or of fitness for a particular purpose.  In no event shall the
* contributor or the ITU be liable for any incidental, punitive, or
* consequential damages of any kind whatsoever arising from the
* use of these programs.
*
* This disclaimer of warranty extends to the user of these programs
* and user's customers, employees, agents, transferees, successors,
* and assigns.
*
* The ITU does not represent or warrant that the programs furnished
* hereunder are free of infringement of any third-party patents.
* Commercial implementations of ITU-T Recommendations, including
* shareware, may be subject to royalty fees to patent holders.
* Information regarding the ITU-T patent policy is available from
* the ITU Web site at http://www.itu.int.
*
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE ITU-T PATENT POLICY.
************************************************************************
*/

/*!
 ***********************************************************************
 * \file macroblock.c
 *
 * \brief
 *     Decode a Macroblock
 *
 * \author
 *    Main contributors (see contributors.h for copyright, address and affiliation details)
 *    - Inge Lille-Lang鴜               <inge.lille-langoy@telenor.com>
 *    - Rickard Sjoberg                 <rickard.sjoberg@era.ericsson.se>
 *    - Jani Lainema                     <jani.lainema@nokia.com>
 *    - Sebastian Purreiter             <sebastian.purreiter@mch.siemens.de>
 *    - Thomas Wedi                     <wedi@tnt.uni-hannover.de>
 *    - Detlev Marpe                      <marpe@hhi.de>
 *    - Gabi Blaettermann              <blaetter@hhi.de>
 *    - Ye-Kui Wang                      <wangy@cs.tut.fi>
 ***********************************************************************
*/

#include "contributors.h"

#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#include "global.h"
#include "mbuffer.h"
#include "elements.h"
#include "macroblock.h"


/*!
 ************************************************************************
 * \brief
 *    Checks the availability of neighboring macroblocks of
 *    the current macroblock for prediction and context determination;
 *    marks the unavailable MBs for intra prediction in the
 *    ipredmode-array by -1. Only neighboring MBs in the causal
 *    past of the current MB are checked.
 ************************************************************************
 */
void CheckAvailabilityOfNeighbors(struct img_par *img)
{
  int i,j;
  const int mb_width = img->width/MB_BLOCK_SIZE;
  const int mb_nr = img->current_mb_nr;
  Macroblock *currMB = &img->mb_data[mb_nr];

  // mark all neighbors as unavailable
  for (i=0; i<3; i++)
    for (j=0; j<3; j++)
      img->mb_data[mb_nr].mb_available[i][j]=NULL;
  img->mb_data[mb_nr].mb_available[1][1]=currMB; // current MB

  // Check MB to the left
  if(img->pix_x >= MB_BLOCK_SIZE)
  {
    int remove_prediction = currMB->slice_nr != img->mb_data[mb_nr-1].slice_nr;
    if(img->UseConstrainedIntraPred)
      remove_prediction = (remove_prediction || img->intra_mb[mb_nr-1] ==0);
    if(remove_prediction)
    {
      img->ipredmode[img->block_x][img->block_y+1] = -1;
      img->ipredmode[img->block_x][img->block_y+2] = -1;
      img->ipredmode[img->block_x][img->block_y+3] = -1;
      img->ipredmode[img->block_x][img->block_y+4] = -1;
    } else
      currMB->mb_available[1][0]=&(img->mb_data[mb_nr-1]);
  }


  // Check MB above
  if(img->pix_y >= MB_BLOCK_SIZE)
  {
    int remove_prediction = currMB->slice_nr != img->mb_data[mb_nr-mb_width].slice_nr;
    if(img->UseConstrainedIntraPred)
      remove_prediction = (remove_prediction || img->intra_mb[mb_nr-mb_width] ==0);
    if(remove_prediction)
    {
      img->ipredmode[img->block_x+1][img->block_y] = -1;
      img->ipredmode[img->block_x+2][img->block_y] = -1;
      img->ipredmode[img->block_x+3][img->block_y] = -1;
      img->ipredmode[img->block_x+4][img->block_y] = -1;
    } else
      currMB->mb_available[0][1]=&(img->mb_data[mb_nr-mb_width]);
  }

  // Check MB left above
  if(img->pix_x >= MB_BLOCK_SIZE && img->pix_y  >= MB_BLOCK_SIZE )
  {
    if(currMB->slice_nr == img->mb_data[mb_nr-mb_width-1].slice_nr)
      img->mb_data[mb_nr].mb_available[0][0]=&(img->mb_data[mb_nr-mb_width-1]);
  }

  // Check MB right above
  if(img->pix_y >= MB_BLOCK_SIZE && img->pix_x < (img->width-MB_BLOCK_SIZE ))
  {
    if(currMB->slice_nr == img->mb_data[mb_nr-mb_width+1].slice_nr)
      currMB->mb_available[0][2]=&(img->mb_data[mb_nr-mb_width+1]);
  }
}

/*!
 ************************************************************************
 * \brief
 *    initializes the current macroblock
 ************************************************************************
 */
void start_macroblock(struct img_par *img,struct inp_par *inp)
{
  int i,j,k,l;
  Macroblock *currMB = &img->mb_data[img->current_mb_nr];

  // WYK: Oct. 8, 2001, start ...
  // The following is moved and modified from exit_macroblock(), 
  // to make the decoding process correct when some macroblocks are lost
  /* Update coordinates of the current macroblock */
  img->mb_x = (img->current_mb_nr)%(img->width/MB_BLOCK_SIZE);
  img->mb_y = (img->current_mb_nr)/(img->width/MB_BLOCK_SIZE);
  
  /* Define vertical positions */
  img->block_y = img->mb_y * BLOCK_SIZE;      /* luma block position */
  img->pix_y   = img->mb_y * MB_BLOCK_SIZE;   /* luma macroblock position */
  img->pix_c_y = img->mb_y * MB_BLOCK_SIZE/2; /* chroma macroblock position */
  
  /* Define horizontal positions */
  img->block_x = img->mb_x * BLOCK_SIZE;      /* luma block position */
  img->pix_x   = img->mb_x * MB_BLOCK_SIZE;   /* luma pixel position */
  img->pix_c_x = img->mb_x * MB_BLOCK_SIZE/2; /* chroma pixel position */
  //WYK: Oct. 8, 2001, ... end

  // Save the slice number of this macroblock. When the macroblock below
  // is coded it will use this to decide if prediction for above is possible
  currMB->slice_nr = img->current_slice_nr;
  
  // If MB is next to a slice boundary, mark neighboring blocks unavailable for prediction
  CheckAvailabilityOfNeighbors(img);

  // Reset syntax element entries in MB struct
  currMB->mb_type = 0;
  currMB->ref_frame = 0;
  currMB->predframe_no = 0;
  currMB->delta_quant = 0;

  currMB->cbp     = 0;
  currMB->cbp_blk = 0;

  for (l=0; l < 2; l++)
    for (j=0; j < BLOCK_MULTIPLE; j++)
      for (i=0; i < BLOCK_MULTIPLE; i++)
        for (k=0; k < 2; k++)
          currMB->mvd[l][j][i][k] = 0;

  for (i=0; i < (BLOCK_MULTIPLE*BLOCK_MULTIPLE); i++)
    currMB->intra_pred_modes[i] = 0;
}

/*!
 ************************************************************************
 * \brief
 *    set coordinates of the next macroblock
 *    check end_of_slice condition (have to implement)
 ************************************************************************
 */
int exit_macroblock(struct img_par *img,struct inp_par *inp)
{
  const int number_mb_per_row = img->width / MB_BLOCK_SIZE ;
  Slice *currSlice = img->currentSlice;

  // Update coordinates of the next macroblock
  img->mb_x++;
  if (img->mb_x == number_mb_per_row) // next row of MBs
  {
    img->mb_x = 0; // start processing of next row
    img->mb_y++;
  }
  img->current_mb_nr++;

  // Define vertical positions
  img->block_y = img->mb_y * BLOCK_SIZE;      // luma block position
  img->pix_y   = img->mb_y * MB_BLOCK_SIZE;   // luma macroblock position
  img->pix_c_y = img->mb_y * MB_BLOCK_SIZE/2; // chroma macroblock position

  // Define horizontal positions
  img->block_x = img->mb_x * BLOCK_SIZE;      // luma block position
  img->pix_x   = img->mb_x * MB_BLOCK_SIZE;   // luma pixel position
  img->pix_c_x = img->mb_x * MB_BLOCK_SIZE/2; // chroma pixel position

  if (img->current_mb_nr == img->max_mb_nr)
  {
    if (currSlice->next_header != EOS)
      currSlice->next_header = SOP;
    return TRUE;
  }
  // ask for last mb in the slice  UVLC
  else
  {
    if(nal_startcode_follows(img, inp) == FALSE)
      return FALSE;
    if(img->type == INTRA_IMG || img->type == SP_IMG_1|| img->type == SP_IMG_MULT || inp->symbol_mode == CABAC)
      return TRUE;
    if(img->cod_counter<=0)
      return TRUE;
    return FALSE;
  }
}

/*!
 ************************************************************************
 * \brief
 *    Interpret the mb mode for P-Frames
 ************************************************************************
 */
void interpret_mb_mode_P(struct img_par *img)
{
  const int ICBPTAB[6] = {0,16,32,15,31,47};
  Macroblock *currMB = &img->mb_data[img->current_mb_nr];


  currMB->intraOrInter           = INTER_MB ;

  if (img->mb_mode == INTRA_MB)   // 4x4 intra
  {
    currMB->intraOrInter         = INTRA_MB_4x4 ;
    currMB->mb_imode = img->imod = INTRA_MB_OLD ;
  }
  if (img->mb_mode > INTRA_MB)    // 16x16 intra
  {
    currMB->intraOrInter         = INTRA_MB_16x16 ;
    img->imod = currMB->mb_imode = INTRA_MB_NEW;     // mod0=img->mb_mode-1;kmod=mod0 & 3;cbp = ICBPTAB[mod0/4];
    currMB->intra_pred_modes[0] = (img->mb_mode - INTRA_MB-1) & 3;
    currMB->cbp = ICBPTAB[(img->mb_mode - INTRA_MB-1)>>2];
  }
  if (img->mb_mode < INTRA_MB)    // inter prediction mode (block shape)
    img->imod = currMB->mb_imode = INTRA_MB_INTER;   // intra in inter frame
}

/*!
 ************************************************************************
 * \brief
 *    Interpret the mb mode for I-Frames
 ************************************************************************
 */
void interpret_mb_mode_I(struct img_par *img)
{
  const int ICBPTAB[6] = {0,16,32,15,31,47};
  Macroblock *currMB   = &img->mb_data[img->current_mb_nr];

  if (img->mb_mode == 0)
  {
    currMB->intraOrInter         = INTRA_MB_4x4 ;
    img->imod = currMB->mb_imode = INTRA_MB_OLD;     // 4x4 intra
  }
  else
  {
    currMB->intraOrInter         = INTRA_MB_16x16 ;
    img->imod = currMB->mb_imode = INTRA_MB_NEW;     // 16x16 intra */ /* mod0=img->mb_mode-1;kmod=mod0 & 3;cbp = ICBPTAB[mod0/4];
    currMB->intra_pred_modes[0] = (img->mb_mode - 1) & 3;
    currMB->cbp = ICBPTAB[(img->mb_mode - 1)>>2];
  }
}

/*!
 ************************************************************************
 * \brief
 *    Interpret the mb mode for B-Frames
 ************************************************************************
 */
void interpret_mb_mode_B(struct img_par *img)
{
  const int ICBPTAB[6] = {0,16,32,15,31,47};
  Macroblock *currMB = &img->mb_data[img->current_mb_nr];

  currMB->intraOrInter         = INTER_MB ;
  if (img->mb_mode == INTRA_MB_B) // 4x4 intra
  {
    currMB->intraOrInter         = INTRA_MB_4x4 ;
    img->imod = currMB->mb_imode = INTRA_MB_OLD;
  }
  if (img->mb_mode > INTRA_MB_B)  // 16x16 intra
  {
    currMB->intraOrInter         = INTRA_MB_16x16 ;
    img->imod = currMB->mb_imode = INTRA_MB_NEW;
    currMB->intra_pred_modes[0] = (img->mb_mode - INTRA_MB_B-1) & 3;
    currMB->cbp = ICBPTAB[(img->mb_mode - INTRA_MB_B-1)>>2];
  }

  if (img->mb_mode < INTRA_MB_B)
  {
    if(img->mb_mode == 0)
      img->imod = currMB->mb_imode = B_Direct;
    else if(img->mb_mode == 3)
      img->imod = currMB->mb_imode = B_Bidirect;
    else if(img->mb_mode==1 || (img->mb_mode>3 && img->mb_mode%2==0))
      img->imod = currMB->mb_imode = B_Forward;
    else if(img->mb_mode==2 || (img->mb_mode>4 && img->mb_mode%2==1))
      img->imod = currMB->mb_imode = B_Backward;
  }
}

/*!
 ************************************************************************
 * \brief
 *    init macroblock I and P frames
 ************************************************************************
 */
void init_macroblock(struct img_par *img)
{
  int i,j;
  int predframe_no;
  Macroblock *currMB = &img->mb_data[img->current_mb_nr];

  img->mv[img->block_x+4][img->block_y][2]=img->number;

  for (i=0;i<BLOCK_SIZE;i++)
  {                           // reset vectors and pred. modes
    for(j=0;j<BLOCK_SIZE;j++)
    {
      img->mv[img->block_x+i+4][img->block_y+j][0] = 0;
      img->mv[img->block_x+i+4][img->block_y+j][1] = 0;
      img->ipredmode[img->block_x+i+1][img->block_y+j+1] = 0;
    }
  }

  currMB->ref_frame = 0;
  currMB->predframe_no = predframe_no = 0;// g.b.1;

  // Set the reference frame information for motion vector prediction
  if (img->imod == INTRA_MB_OLD || img->imod == INTRA_MB_NEW)
    for (j = 0;j < BLOCK_SIZE;j++)
      for (i = 0;i < BLOCK_SIZE;i++)
        refFrArr[img->block_y+j][img->block_x+i] = -1;
  else
    for (j = 0;j < BLOCK_SIZE;j++)
      for (i = 0;i < BLOCK_SIZE;i++)
        refFrArr[img->block_y+j][img->block_x+i] = predframe_no;
}

/*!
 ************************************************************************
 * \brief
 *    Get the syntax elements from the NAL
 ************************************************************************
 */
int read_one_macroblock(struct img_par *img,struct inp_par *inp)
{
  int i, i1, j1;

  SyntaxElement currSE;
  Macroblock *currMB = &img->mb_data[img->current_mb_nr];

  Slice *currSlice = img->currentSlice;
  DataPartition *dP;
  int *partMap = assignSE2partition[currSlice->dp_mode];

  int dbl_ipred_word;

  currMB->qp           = img->qp ;

⌨️ 快捷键说明

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