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

📄 macroblock.c

📁 AVS编解码是学习AVS程序开发的入门资料,可以帮助初学者获得很多的收获.
💻 C
📖 第 1 页 / 共 5 页
字号:
     /*
*****************************************************************************
* COPYRIGHT AND WARRANTY INFORMATION
*
* Copyright 2003, Advanced Audio Video Coding Standard, Part II
*
* DISCLAIMER OF WARRANTY
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations under
* the License.
*                     
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE AVS PATENT POLICY.
* The AVS Working Group doesn't represent or warrant that the programs
* furnished here under are free of infringement of any third-party patents.
* Commercial implementations of AVS, including shareware, may be
* subject to royalty fees to patent holders. Information regarding
* the AVS patent policy for standardization procedure is available at 
* AVS Web site http://www.avs.org.cn. Patent Licensing is outside
* of AVS Working Group.
*
* The Original Code is Reference Software for China National Standard 
* GB/T 20090.2-2006 (short for AVS-P2 or AVS Video) at version RM52J.
*
* The Initial Developer of the Original Code is Video subgroup of AVS
* Workinggroup (Audio and Video coding Standard Working Group of China).
* Contributors:   Guoping Li,    Siwei Ma,    Jian Lou,    Qiang Wang , 
*   Jianwen Chen,Haiwu Zhao,  Xiaozhen Zheng, Junhao Zheng, Zhiming Wang
* 
******************************************************************************
*/



/*
*************************************************************************************
* File name: macroblock.c
* Function: Decode a Macroblock
*
*************************************************************************************
*/



#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"
#include "vlc.h"

#include "defines.h"
#include "block.h"

void readReferenceIndex(struct img_par *img, struct inp_par *inp);
void readMotionVector(struct img_par *img, struct inp_par *inp);

#define MODE_IS_P8x8  (mbmode==4)
#define MODE_IS_I4x4  (mbmode==5)                  //xfwang modify          2004.7.29
#define I16OFFSET     (mbmode-7)
#define fwd_ref_idx_to_refframe(idx) ((idx)+fwd_refframe_offset)
#define bwd_ref_idx_to_refframe(idx) ((idx)+bwd_refframe_offset)



/*
******************************************************************************
*  Function: calculated field or frame distance between current field(frame)
*            and the reference field(frame).
*     Input: blkref 
			 fw_bw  
*    Output: distance for motion vector scale
*    Return: 
* Attention:
*    Author: Yulejun // 2004.07.14
******************************************************************************
*/
// The funtion int calculate_distance(int blkref, int fw_bw ) is modified by Xiaozhen Zheng, HiSilicon, 20070327. 
// At the modified version, 'img->tr' is replaced by 'picture_distance', which is used to calculate DistanceIndex and BlockDistance.
int calculate_distance(int blkref, int fw_bw )  //fw_bw>=0: forward ; fw_bw<0: backward
{
  int distance;
  if( img->picture_structure == 1 ) //frame coding
  {
    if ( img->type==P_IMG ) // P img
    {
        if(blkref==0)
              distance = picture_distance*2 - img->imgtr_last_P*2;
        else if(blkref==1)
              distance = picture_distance*2 - img->imgtr_last_prev_P*2;
		else
		{
			  assert(0); //only two reference pictures for P frame
		}
    }
    else //B_IMG
    {
		if (fw_bw >=0 ) //forward
			  distance = picture_distance*2 - img->imgtr_last_P*2;
		else
			  distance = img->imgtr_next_P*2  - picture_distance*2;
    }
  }  
  else if( ! img->picture_structure )  //field coding
  {
    if(img->type==P_IMG)
    {
      if(img->top_bot==0) //top field
			{
				switch ( blkref )
				{
				case 0:
					distance = picture_distance*2 - img->imgtr_last_P*2 - 1 ;
					break;
				case 1:
					distance = picture_distance*2 - img->imgtr_last_P*2 ;
					break;
				case 2:
					distance = picture_distance*2 - img->imgtr_last_prev_P*2 - 1;
					break;
				case 3:
					distance = picture_distance*2 - img->imgtr_last_prev_P*2 ;
					break;
				}
			}
        else if(img->top_bot==1) // bottom field.
			{
				switch ( blkref )
				 {
				case 0:
					distance = 1 ;
					break;
				case 1:
					distance = picture_distance*2 - img->imgtr_last_P*2 ;
					break;
				case 2:
					distance = picture_distance*2 - img->imgtr_last_P*2 + 1;
					break;
				case 3:
					distance = picture_distance*2 - img->imgtr_last_prev_P*2 ;
						break;
				  }
			}
		 else 
			{
				printf("Error. frame picture should not run into this branch.");
				exit(-1);
			}
    }
    else if(img->type==B_IMG)
    {
			assert(blkref==0 || blkref == 1);
			if (fw_bw >= 0 ) //forward
			{
				if(img->top_bot==0) //top field
				{
					switch ( blkref )
					{
					case 0:
						distance = picture_distance*2 - img->imgtr_last_P*2 - 1 ;
						break;
					case 1:
						distance = picture_distance*2 - img->imgtr_last_P*2;
						break;
					}
				}
				else if(img->top_bot==1) // bottom field.
				{
				        switch ( blkref )
					{
					case 0:
						distance = picture_distance*2 - img->imgtr_last_P*2 ;
						break;
					case 1:
						distance = picture_distance*2 - img->imgtr_last_P*2 + 1;
						break;
					}
				}
				else 
				{
					printf("Error. frame picture should not run into this branch.");
					exit(-1);
				}
			}
			else // backward
			{
				if(img->top_bot==0) //top field
				{
					switch ( blkref )
					{
						case 0:
							distance = img->imgtr_next_P*2 - picture_distance*2;
							break;
						case 1:
							distance = img->imgtr_next_P*2 - picture_distance*2 + 1;
							break;
					}
				}
				else if(img->top_bot==1) // bottom field.
				{
					switch ( blkref )
					{
					case 0:
						distance = img->imgtr_next_P*2 - picture_distance*2 -  1;
						break;
					case 1:
						distance = img->imgtr_next_P*2 - picture_distance*2 ;
						break;
					}
				}
				else 
				{
					printf("Error. frame picture should not run into this branch.");
					exit(-1);
				}
			}

    }
  }
  distance = (distance+512)%512;	// Added by Xiaozhen ZHENG, 20070413, HiSilicon
  return distance;
}

/*Lou 1016 Start*/
//The unit of time distance is calculated by field time
/*
*************************************************************************
* Function:
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/
int scale_motion_vector(int motion_vector, int currblkref, int neighbourblkref, int currsmbtype, int neighboursmbtype, int block_y_pos, int curr_block_y, int ref, int direct_mv)
{
    int sign = (motion_vector>0?1:-1);
    int mult_distance;
    int devide_distance;

    motion_vector = abs(motion_vector);

    if(motion_vector == 0)
        return 0;


    mult_distance = calculate_distance( currblkref, ref );
    devide_distance = calculate_distance(neighbourblkref, ref);

    motion_vector = sign*((motion_vector*mult_distance*(512/devide_distance)+256)>>9);

    return motion_vector;
}
/*Lou 1016 End*/

/*
*************************************************************************
* Function:Set motion vector predictor
* Input:
* Output:
* Return: 
* Attention:
*************************************************************************
*/
static void SetMotionVectorPredictor (struct img_par  *img,
                                      int             *pmv_x,
                                      int             *pmv_y,
                                      int             ref_frame,
                                      int             **refFrArr,
                                      int             ***tmp_mv,
                                      int             block_x,
                                      int             block_y,
                                      int             blockshape_x,
                                      int             blockshape_y,
				      int             ref,
                                      int             direct_mv)//Lou 1016
{
  int mb_x                 = 8*block_x;
  int mb_y                 = 8*block_y;
  int pic_block_x          = img->block_x + block_x;
  int pic_block_y          = img->block_y + block_y;
  int mb_width             = img->width/16;
  int mb_nr = img->current_mb_nr;
  int mb_available_up   = (img->mb_y == 0 ) ? 0 : (mb_data[mb_nr].slice_nr == mb_data[mb_nr-mb_width  ].slice_nr);  // jlzheng 6.23 
  int mb_available_left = (img->mb_x == 0 ) ? 0 : (mb_data[mb_nr].slice_nr == mb_data[mb_nr-1         ].slice_nr);  // jlzheng 6.23
  int mb_available_upleft  = (img->mb_x == 0) ? 0 : ((img->mb_y == 0) ? 0 : 
  (mb_data[mb_nr].slice_nr == mb_data[mb_nr-mb_width-1].slice_nr));
  int mb_available_upright = (img->mb_y == 0) ? 0 : ((img->mb_x >= (mb_width-1)) ? 0 :
  (mb_data[mb_nr].slice_nr == mb_data[mb_nr-mb_width+1].slice_nr));
  
  int block_available_up, block_available_left, block_available_upright, block_available_upleft;
  int mv_a, mv_b, mv_c, mv_d, pred_vec=0;
  int mvPredType, rFrameL, rFrameU, rFrameUR;
  int hv;
  int mva[3] , mvb[3],mvc[3];
	int y_up = 1,y_upright=1,y_upleft=1,off_y=0;	
    /*Lou 1016 Start*/
  int rFrameUL;
  Macroblock*     currMB = &mb_data[img->current_mb_nr];
  int smbtypecurr, smbtypeL, smbtypeU, smbtypeUL, smbtypeUR;
 

    smbtypecurr = -2;
    smbtypeL = -2;
    smbtypeU = -2;
    smbtypeUL = -2;
    smbtypeUR = -2;
  /*Lou 1016 End*/

  /* D B C */
  /* A X   */
  
  /* 1 A, B, D are set to 0 if unavailable       */
  /* 2 If C is not available it is replaced by D */

   block_available_up   = mb_available_up   || (mb_y > 0);
   block_available_left = mb_available_left || (mb_x > 0);
  
  if (mb_y > 0)
  {
    if (mb_x < 8)  // first column of 8x8 blocks
    {
      if (mb_y==8)
      {
        if (blockshape_x == 16)      block_available_upright = 0;
        else                         block_available_upright = 1;
      }
      else
      {
        if (mb_x+blockshape_x != 8)  block_available_upright = 1;
        else                         block_available_upright = 0;
      }
    }
    else
    {
      if (mb_x+blockshape_x != 16)   block_available_upright = 1;
      else                           block_available_upright = 0;
    }
  }
  else if (mb_x+blockshape_x != MB_BLOCK_SIZE)
  {
    block_available_upright = block_available_up;
  }
  else
  {
    block_available_upright = mb_available_upright;
  }
  
  if (mb_x > 0)
  {
    block_available_upleft = (mb_y > 0 ? 1 : mb_available_up);
  }
  else if (mb_y > 0)
  {
    block_available_upleft = mb_available_left;
  }
  else
  {
    block_available_upleft = mb_available_upleft;
  }

    smbtypecurr = -2;
    smbtypeL = -2;
    smbtypeU = -2;
    smbtypeUL = -2;
    smbtypeUR = -2;

    mvPredType = MVPRED_MEDIAN;

    rFrameL    = block_available_left    ? refFrArr[pic_block_y]  [pic_block_x-1] : -1;
    rFrameU    = block_available_up      ? refFrArr[pic_block_y-1][pic_block_x]   : -1;
    rFrameUR   = block_available_upright ? refFrArr[pic_block_y-1][pic_block_x+blockshape_x/8] :
    block_available_upleft  ? refFrArr[pic_block_y-1][pic_block_x-1] : -1;
    rFrameUL   = block_available_upleft  ? refFrArr[pic_block_y-1][pic_block_x-1] : -1;

  if((rFrameL != -1)&&(rFrameU == -1)&&(rFrameUR == -1))
    mvPredType = MVPRED_L;
  else if((rFrameL == -1)&&(rFrameU != -1)&&(rFrameUR == -1))
    mvPredType = MVPRED_U;
  else if((rFrameL == -1)&&(rFrameU == -1)&&(rFrameUR != -1))
    mvPredType = MVPRED_UR;
  /*Lou 1016 End*/   
  // Directional predictions 
  else if(blockshape_x == 8 && blockshape_y == 16)
  {
    if(mb_x == 0)
    {
      if(rFrameL == ref_frame)
        mvPredType = MVPRED_L;
    }
    else
    {
      if( rFrameUR == ref_frame)
        mvPredType = MVPRED_UR;
    }
  }
  else if(blockshape_x == 16 && blockshape_y == 8)
  {
    if(mb_y == 0)
    {
      if(rFrameU == ref_frame)
        mvPredType = MVPRED_U;
    }
    else
    {
      if(rFrameL == ref_frame)

⌨️ 快捷键说明

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