📄 macroblock.c
字号:
/*
***********************************************************************
* COPYRIGHT AND WARRANTY INFORMATION
*
* Copyright 2003, Advanced Audio Video Coding Standard, Part II
*
* DISCLAIMER OF WARRANTY
*
* These software programs are available to the users without any
* license fee or royalty on an "as is" basis. The AVS 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 contributors or
* the AVS be liable for any incidental, punitive, or consequential
* damages of any kind whatsoever arising from the use of this program.
*
* This disclaimer of warranty extends to the user of this program
* and user's customers, employees, agents, transferees, successors,
* and assigns.
*
* The AVS does not represent or warrant that the program furnished
* hereunder 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 is available from the AVS Web site at
* http://www.avs.org.cn
*
* THIS IS NOT A GRANT OF PATENT RIGHTS - SEE THE AVS PATENT POLICY.
************************************************************************
*/
/*
*************************************************************************************
* 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:
* 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
/*
*************************************************************************
* Function: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.
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
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 = &mb_data[mb_nr];
int check_value;
// mark all neighbors as unavailable
for (i=0; i<3; i++)
for (j=0; j<3; j++)
mb_data[mb_nr].mb_available[i][j]=NULL;
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 != mb_data[mb_nr-1].slice_nr;
// upper blocks
if (remove_prediction)
{
img->ipredmode[img->block_x][img->block_y+1] = -1;
img->ipredmode[img->block_x][img->block_y+2] = -1;
}
if (!remove_prediction)
{
currMB->mb_available[1][0]=&(mb_data[mb_nr-1]);
}
}
// Check MB above
check_value = (img->pix_y >= MB_BLOCK_SIZE);
if(check_value)
{
int remove_prediction = currMB->slice_nr != mb_data[mb_nr-mb_width].slice_nr;
// upper blocks
if (remove_prediction)
{
img->ipredmode[img->block_x+1][img->block_y] = -1;
img->ipredmode[img->block_x+2][img->block_y] = -1;
}
if (!remove_prediction)
{
currMB->mb_available[0][1]=&(mb_data[mb_nr-mb_width]);
}
}
// Check MB left above
if(img->pix_y >= MB_BLOCK_SIZE && img->pix_x >= MB_BLOCK_SIZE)
{
int remove_prediction = currMB->slice_nr != mb_data[mb_nr-mb_width-1].slice_nr;
if (remove_prediction)
{
img->ipredmode[img->block_x][img->block_y] = -1;
}
if (!remove_prediction)
{
currMB->mb_available[0][0]=&(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 == mb_data[mb_nr-mb_width+1].slice_nr)
currMB->mb_available[0][2]=&(mb_data[mb_nr-mb_width+1]);
}
}
void set_MB_parameters (struct img_par *img,struct inp_par *inp, int mb)
{
const int number_mb_per_row = img->width / MB_BLOCK_SIZE ;
const int mb_nr = img->current_mb_nr;
Macroblock *currMB = &mb_data[mb_nr];
img->mb_x = mb % number_mb_per_row;
img->mb_y = mb / number_mb_per_row;
// Define vertical positions
img->block8_y= img->mb_y * BLOCK_SIZE/2;
img->block_y = img->mb_y * BLOCK_SIZE/2; // vertical luma block position
img->pix_y = img->mb_y * MB_BLOCK_SIZE; // vertical luma macroblock position
img->pix_c_y = img->mb_y * MB_BLOCK_SIZE/2; // vertical chroma macroblock position
// Define horizontal positions
img->block8_x= img->mb_x * BLOCK_SIZE/2;
img->block_x = img->mb_x * BLOCK_SIZE/2; // luma block
img->pix_x = img->mb_x * MB_BLOCK_SIZE; // luma pixel
img->pix_c_x = img->mb_x * MB_BLOCK_SIZE/2; // chroma pixel
}
/*
*************************************************************************
* Function:initializes the current macroblock
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
void start_macroblock(struct img_par *img,struct inp_par *inp)
{
int i,j,k,l;
Macroblock *currMB; // intialization code deleted, see below, StW
assert (img->current_mb_nr >=0 && img->current_mb_nr < img->max_mb_nr);
currMB = &mb_data[img->current_mb_nr];//GB
/* 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/2; /* luma block position */
img->block8_y = img->mb_y * BLOCK_SIZE/2;
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/2; /* luma block position */
img->block8_x = img->mb_x * BLOCK_SIZE/2;
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 MB is next to a slice boundary, mark neighboring blocks unavailable for prediction
CheckAvailabilityOfNeighbors(img); // support only slice mode 0 in MBINTLC1 at this time
// Reset syntax element entries in MB struct
currMB->qp = img->qp ;
currMB->mb_type = 0;
currMB->delta_quant = 0;
currMB->cbp = 0;
currMB->cbp_blk = 0;
currMB->c_ipred_mode= DC_PRED_8; //GB
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;
currMB->cbp_bits = 0;
// initialize img->m7 for ABT//Lou
for (j=0; j<MB_BLOCK_SIZE; j++)
for (i=0; i<MB_BLOCK_SIZE; i++)
img->m7[i][j] = 0;
for (j=0; j<BLOCK_SIZE; j++)
for (i=0; i<BLOCK_SIZE; i++)
{
img->m8[0][i][j] = 0;
img->m8[1][i][j] = 0;
}
currMB->lf_disable = loop_filter_disable;
img->weighting_prediction=0; //cjw 20051230 default value Weighting Predicition is 0
}
/*
*************************************************************************
* Function:Interpret the mb mode for P-Frames
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
void interpret_mb_mode_P(struct img_par *img)
{
int i;
const int ICBPTAB[6] = {0,16,32,15,31,47};
Macroblock *currMB = &mb_data[img->current_mb_nr];//GB current_mb_nr];
int mbmode = currMB->mb_type;
if(mbmode <4)
{
currMB->mb_type = mbmode;
for (i=0;i<4;i++)
{
currMB->b8mode[i] = mbmode;
currMB->b8pdir[i] = 0;
}
}
else if(MODE_IS_P8x8)
{
currMB->mb_type = P8x8;
//img->allrefzero = ZERO_P8x8; //delete by xfwang 2004.7.29
}
else if(/* MODE_IS_I4x4 qhg */mbmode>=5)//modefy by xfwang 2004.7.29
{
currMB->cbp=NCBP[currMB->mb_type-5][0]; // qhg //modefy by xfwang 2004.7.29
currMB->mb_type = I4MB;
for (i=0;i<4;i++)
{
currMB->b8mode[i] = IBLOCK;
currMB->b8pdir[i] = -1;
}
}
else
{
currMB->mb_type = I16MB;
for (i=0;i<4;i++) {currMB->b8mode[i]=0; currMB->b8pdir[i]=-1; }
currMB->cbp= ICBPTAB[(I16OFFSET)>>2];
}
}
/*
*************************************************************************
* Function:Interpret the mb mode for I-Frames
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
void interpret_mb_mode_I(struct img_par *img)
{
int i;
const int ICBPTAB[6] = {0,16,32,15,31,47};
Macroblock *currMB = &mb_data[img->current_mb_nr];
int num =4;
currMB->mb_type = I4MB;
for (i=0;i<4;i++)
{
currMB->b8mode[i]=IBLOCK;
currMB->b8pdir[i]=-1;
}
for (i=num;i<4;i++)
{
currMB->b8mode[i]=currMB->mb_type_2==P8x8? 4 : currMB->mb_type_2; currMB->b8pdir[i]=0;
}
}
/*
*************************************************************************
* Function:Interpret the mb mode for B-Frames
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
void interpret_mb_mode_B(struct img_par *img)
{
static const int offset2pdir16x16[12] = {0, 0, 1, 2, 0,0,0,0,0,0,0,0};
static const int offset2pdir16x8[22][2] = {{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{1,1},{0,0},{0,1},{0,0},{1,0},
{0,0},{0,2},{0,0},{1,2},{0,0},{2,0},{0,0},{2,1},{0,0},{2,2},{0,0}};
static const int offset2pdir8x16[22][2] = {{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{1,1},{0,0},{0,1},{0,0},
{1,0},{0,0},{0,2},{0,0},{1,2},{0,0},{2,0},{0,0},{2,1},{0,0},{2,2}};
const int ICBPTAB[6] = {0,16,32,15,31,47};
Macroblock *currMB = &mb_data[img->current_mb_nr];//GB current_mb_nr];
int i, mbmode;
int mbtype = currMB->mb_type;
int *b8mode = currMB->b8mode;
int *b8pdir = currMB->b8pdir;
//--- set mbtype, b8type, and b8pdir ---
if (mbtype==0) // direct
{
mbmode=0; for(i=0;i<4;i++) {b8mode[i]=0; b8pdir[i]=2; }
}
else if (/*mbtype==23 qhg */ mbtype>=23) // intra4x4
{
currMB->cbp=NCBP[mbtype-23][0]; // qhg
mbmode=I4MB; for(i=0;i<4;i++) {b8mode[i]=IBLOCK; b8pdir[i]=-1; }
}
else if (mbtype==22) // 8x8(+split)
{
mbmode=P8x8; // b8mode and pdir is transmitted in additional codewords
}
else if (mbtype<4) // 16x16
{
mbmode=1;
for(i=0;i<4;i++) {b8mode[i]=1; b8pdir[i]=offset2pdir16x16[mbtype]; }
}
else if (mbtype%2==0) // 16x8
{
mbmode=2;
for(i=0;i<4;i++) {b8mode[i]=2; b8pdir[i]=offset2pdir16x8 [mbtype][i/2]; }
}
else
{
mbmode=3;
for(i=0;i<4;i++) {b8mode[i]=3; b8pdir[i]=offset2pdir8x16 [mbtype][i%2]; }
}
currMB->mb_type = mbmode;
}
/*
*************************************************************************
* Function:init macroblock I and P frames
* Input:
* Output:
* Return:
* Attention:
*************************************************************************
*/
void init_macroblock(struct img_par *img)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -