📄 dmbpbframe.cpp
字号:
#include "stdafx.h"
#include "color.h"
#include "DBitstream.h"
#include "sactable.h"
#include "sad.h"
#include "Dh263class.h"
#include "Dhuffman.h"
#include "commonfunc.h"
extern "C"
{
void MMXLoadBlock(BYTE *MBPtr,BYTE *ImgPtr,int pixel);
void MMXLoadMB(BYTE *MBPtr,BYTE *ImgPtr,int pixel);
void MMXLoadMBData(short *MBPtr, short *Data);
void MMXSetMBDataZero(short *MBPtr);
void MMXSetBlockZero(short *MBPtr);
void MMXDeQuant(short *TempQC,short *rcoeff,int QP);
void MMXStoreMB(BYTE *Loss,short *Data,int pixel);
void MMXStoreBlock(BYTE *Loss,short *Data,int pixel);
void MMXReconBlock(BYTE *Select,BYTE *Source,short *Data,int pixel);
void MMXReconMB(BYTE *Select,BYTE *Source,short *Data,int pixel);
void MMXReconAPBlock(BYTE *Select,BYTE *Source,short *Data,int pixel);
}
#define SIGN(a) ((a)<0 ? -1 : 1)
extern int AndValue[6];
void DH263MB::DecodeBData(void)
{
short iBlockData[68],rCoeffData[68];
short *iblock=iBlockData,*rcoeff=rCoeffData;
if ((int)iblock%8)
iblock+=(4-(int)iblock%8/2);
if ((int)rcoeff%8)
rcoeff+=(4-(int)rcoeff%8/2);
if (MBInfo.CBPB&32)
{BDeQuant(rcoeff,0);
idct(rcoeff,iblock);
MMXLoadMBData(BData.lum,iblock);
}
else
MMXSetMBDataZero(BData.lum);
if (MBInfo.CBPB&16)
{BDeQuant(rcoeff,1);
idct(rcoeff,iblock);
MMXLoadMBData(BData.lum+8,iblock);
}
else
MMXSetMBDataZero(BData.lum+8);
if (MBInfo.CBPB&8)
{BDeQuant(rcoeff,2);
idct(rcoeff,iblock);
MMXLoadMBData(BData.lum+128,iblock);
}
else
MMXSetMBDataZero(BData.lum+128);
if (MBInfo.CBPB&4)
{BDeQuant(rcoeff,3);
idct(rcoeff,iblock);
MMXLoadMBData(BData.lum+136,iblock);
}
else
MMXSetMBDataZero(BData.lum+136);
if (MBInfo.CBPB&2)
{BDeQuant(rcoeff,4);
idct(rcoeff,BData.CB);
}
else
MMXSetBlockZero(BData.CB);
if (MBInfo.CBPB&1)
{BDeQuant(rcoeff,5);
idct(rcoeff,BData.CR);
}
else
MMXSetBlockZero(BData.CR);
}
void DH263MB::BDeQuant(short *rcoeff,int BlockNo)
{
short *TempBQC=BQCoeff+64*BlockNo;
MMXDeQuant(TempBQC,rcoeff,QP_B);
}
int DH263MB::DecodeBCoeff(DBitStream *InputStream)
{ int i;
HuffmanDec VLDec(InputStream);
for (i=0;i<6;i++)
if (MBInfo.CBPB&AndValue[i])
VLDec.GetInterBlock(BQCoeff+64*i);
return 0;
}
int DH263MB::SACDecodeBCoeff(SAD *SADeco)
{ int i;
for (i=0;i<6;i++)
if (MBInfo.CBPB&AndValue[i])
SADeco->GetInterBlock(BQCoeff+64*i);
return 0;
}
void DH263MB::MB_Recon_B(YUVData BLoss,YUVData PrevInter,YUVData CurLoss,
int x, int y,int TRD,int TRB)
{ int RefWidth,Ori,VectorH,VectorW;
int vmx,vmy;
int nh,nv,xstart,xstop,ystart,ystop;
int MVCHR[16]={0,0,0,1,1,1,1,1,1,1,1,1,1,1,2,2};
MotionVector TempMV,MVB;
BYTE *pLoss,*BCurLoss;
//Note:The following program of PB-Frame Mode is
//designed only for the condition when TRD=2&TRB=1.
//That is only one frame is skipped as B-frame.
if (Pic.UMVMode)
RefWidth=Width+32;
else
RefWidth=Width;
Ori=y/MB_SIZE*WMB+x/MB_SIZE;
BCurLoss=BLoss.Y+y*Width+x;
VectorW=TRB*(2*(m_pPic->DMV)[Ori].x+(m_pPic->DMV)[Ori].x_half)/TRD;
VectorH=TRB*(2*(m_pPic->DMV)[Ori].y+(m_pPic->DMV)[Ori].y_half)/TRD;
vmx=(m_pPic->DMVPB)[Ori].x*2+(m_pPic->DMVPB)[Ori].x_half+VectorW;
vmy=(m_pPic->DMVPB)[Ori].y*2+(m_pPic->DMVPB)[Ori].y_half+VectorH;
//Luminance component Bi-directional prediction
LoadRefer(PrevInter.Y,PredData.lum,y*2+vmy,x*2+vmx,2*RefWidth,MB_SIZE,m_pPic->StrPrevInt);
pLoss=CurLoss.Y+y*Width+x;
MVB.x=(vmx-2*(m_pPic->DMV)[Ori].x-(m_pPic->DMV)[Ori].x_half)/2;
MVB.x_half=(vmx-2*(m_pPic->DMV)[Ori].x-(m_pPic->DMV)[Ori].x_half)%2;
MVB.y=(vmy-2*(m_pPic->DMV)[Ori].y-(m_pPic->DMV)[Ori].y_half)/2;
MVB.y_half=(vmy-2*(m_pPic->DMV)[Ori].y-(m_pPic->DMV)[Ori].y_half)%2;
for (nv = 0; nv <= 1; nv++)
for (nh = 0; nh <= 1; nh++)
{ystart=nv*8 + max(0,(-MVB.y*2-MVB.y_half+1)/2-nv*8);
ystop=nv*8 + min(7,15-(MVB.y*2+MVB.y_half+1)/2-nv*8);
xstart=nh*8 + max(0,(-MVB.x*2-MVB.x_half+1)/2-nh*8);
xstop=nh*8 + min(7,15-(MVB.x*2+MVB.x_half+1)/2-nh*8);
BiDirLumPred(ystart,ystop,xstart,xstop,pLoss,MVB);
}
MMXReconMB(BCurLoss,PredData.lum,BData.lum,Width);
//CHR component Forward directional prediction
TempMV.x=vmx/2;
TempMV.x_half=vmx%2;
TempMV.y=vmy/2;
TempMV.y_half=vmy%2;
if (TempMV.x_half)
{if (vmx>=0)
VectorW=TempMV.x+1-TempMV.x%2;
else
VectorW=TempMV.x-1-TempMV.x%2;
}
else
VectorW=TempMV.x;
if (TempMV.y_half)
{if (vmy>=0)
VectorH=TempMV.y+1-TempMV.y%2;
else
VectorH=TempMV.y-1-TempMV.y%2;
}
else
VectorH=TempMV.y;
BCurLoss=BLoss.U+y/2*Width/2+x/2;
LoadCBRefer(PrevInter.U,PredData.CB,y+VectorH,x+VectorW,RefWidth,MB_SIZE/2,m_pPic->StrPrevInt);
BCurLoss=BLoss.V+y/2*Width/2+x/2;
LoadCRRefer(PrevInter.V,PredData.CR,y+VectorH,x+VectorW,RefWidth,MB_SIZE/2,m_pPic->StrPrevInt);
VectorW=2*MVB.x+MVB.x_half;
VectorH=2*MVB.y+MVB.y_half;
if (MVB.x_half)
{if (VectorW>=0)
{MVB.x=(MVB.x+1-MVB.x%2)/2;
MVB.x_half=(MVB.x+1-MVB.x%2)%2;
}
else
{MVB.x=(MVB.x-1-MVB.x%2)/2;
MVB.x_half=(MVB.x-1-MVB.x%2)%2;
}
}
else
{MVB.x=MVB.x/2;
MVB.x_half=MVB.x%2;
}
if (MVB.y_half)
{if (VectorH>=0)
{MVB.y=(MVB.y+1-MVB.y%2)/2;
MVB.y_half=(MVB.y+1-MVB.y%2)%2;
}
else
{MVB.y=(MVB.y-1-MVB.y%2)/2;
MVB.y_half=(MVB.y-1-MVB.y%2)%2;
}
}
else
{MVB.y=MVB.y/2;
MVB.y_half=MVB.y%2;
}
//CHR component bi-directional prediction
pLoss=CurLoss.U+y/2*Width/2+x/2;
ystart=max(0,(-MVB.y*2-MVB.y_half+1)/2);
ystop=min(7,7-(MVB.y*2+MVB.y_half+1)/2);
xstart=max(0,(-MVB.x*2-MVB.x_half+1)/2);
xstop=min(7,7-(MVB.x*2+MVB.x_half+1)/2);
BiDirCBPred(ystart,ystop,xstart,xstop,pLoss,MVB);
BCurLoss=BLoss.U+y/2*Width/2+x/2;
MMXReconBlock(BCurLoss,PredData.CB,BData.CB,Width/2);
//CR Component Bi-directional prediction
pLoss=CurLoss.V+y/2*Width/2+x/2;
BiDirCRPred(ystart,ystop,xstart,xstop,pLoss,MVB);
BCurLoss=BLoss.V+y/2*Width/2+x/2;
MMXReconBlock(BCurLoss,PredData.CR,BData.CR,Width/2);
}
void DH263MB::MB_Recon_B4V(YUVData BLoss,YUVData PrevInter,YUVData CurLoss,
int x, int y,int TRD,int TRB)
{ int RefWidth,Ori,VectorH,VectorW;
int k,vmx,vmy;
int nh,nv,xstart,xstop,ystart,ystop;
int MVCHR[16]={0,0,0,1,1,1,1,1,1,1,1,1,1,1,2,2};
MotionVector MVB;
BYTE *pLoss,*BCurLoss;
//Note:The following program of PB-Frame Mode is
//designed only for the condition when TRD=2&TRB=1.
//That is only one frame is skipped as B-frame.
if (Pic.UMVMode)
RefWidth=Width+32;
else
RefWidth=Width;
Ori=y/MB_SIZE*WMB+x/MB_SIZE;
vmx=(m_pPic->DMVPB)[Ori].x*2+(m_pPic->DMVPB)[Ori].x_half;
vmy=(m_pPic->DMVPB)[Ori].y*2+(m_pPic->DMVPB)[Ori].y_half;
for (k=0;k<4;k++)
{VectorW=TRB*(2*(m_pPic->DMVAP)[Ori][k].x+(m_pPic->DMVAP)[Ori][k].x_half)/TRD+vmx;
VectorH=TRB*(2*(m_pPic->DMVAP)[Ori][k].y+(m_pPic->DMVAP)[Ori][k].y_half)/TRD+vmy;
LoadRefer(PrevInter.Y,PredData.lum+k*64,(y+k/2*MB_SIZE/2)*2+VectorH,
(x+k%2*MB_SIZE/2)*2+VectorW,2*RefWidth,MB_SIZE/2,m_pPic->StrPrevInt);
}
MVB.x=(vmx-2*(m_pPic->DMV)[Ori].x-(m_pPic->DMV)[Ori].x_half)/2;
MVB.x_half=(vmx-2*(m_pPic->DMV)[Ori].x-(m_pPic->DMV)[Ori].x_half)%2;
MVB.y=(vmy-2*(m_pPic->DMV)[Ori].y-(m_pPic->DMV)[Ori].y_half)/2;
MVB.y_half=(vmy-2*(m_pPic->DMV)[Ori].y-(m_pPic->DMV)[Ori].y_half)%2;
pLoss=CurLoss.Y+y*Width+x;
for (nv = 0; nv <= 1; nv++)
for (nh = 0; nh <= 1; nh++)
{MVB.x=(vmx+(TRB-TRD)*(2*(m_pPic->DMVAP)[Ori][nv*2+nh].x+(m_pPic->DMVAP)[Ori][nv*2+nh].x_half)/TRD)/2;
MVB.x_half=(vmx+(TRB-TRD)*(2*(m_pPic->DMVAP)[Ori][nv*2+nh].x+(m_pPic->DMVAP)[Ori][nv*2+nh].x_half)/TRD)%2;
MVB.y=(vmy+(TRB-TRD)*(2*(m_pPic->DMVAP)[Ori][nv*2+nh].y+(m_pPic->DMVAP)[Ori][nv*2+nh].y_half)/TRD)/2;
MVB.y_half=(vmy+(TRB-TRD)*(2*(m_pPic->DMVAP)[Ori][nv*2+nh].y+(m_pPic->DMVAP)[Ori][nv*2+nh].y_half)/TRD)%2;
ystart=nv*8 + max(0,(-MVB.y*2-MVB.y_half+1)/2-nv*8);
ystop=nv*8 + min(7,15-(MVB.y*2+MVB.y_half+1)/2-nv*8);
xstart=nh*8 + max(0,(-MVB.x*2-MVB.x_half+1)/2-nh*8);
xstop=nh*8 + min(7,15-(MVB.x*2+MVB.x_half+1)/2-nh*8);
AP_BiDirLumPred(ystart,ystop,xstart,xstop,pLoss,MVB);
}
BCurLoss=BLoss.Y+y*Width+x;
for (k=0;k<4;k++)
MMXReconAPBlock(BCurLoss+k/2*MB_SIZE/2*Width+k%2*MB_SIZE/2,
PredData.lum+k*64,
BData.lum+k/2*128+k%2*8,
Width);
VectorW=TRB*(2*(m_pPic->DMVAP)[Ori][0].x+(m_pPic->DMVAP)[Ori][0].x_half
+2*(m_pPic->DMVAP)[Ori][1].x+(m_pPic->DMVAP)[Ori][1].x_half
+2*(m_pPic->DMVAP)[Ori][2].x+(m_pPic->DMVAP)[Ori][2].x_half
+2*(m_pPic->DMVAP)[Ori][3].x+(m_pPic->DMVAP)[Ori][3].x_half)/TRD+4*vmx;
VectorH=TRB*(2*(m_pPic->DMVAP)[Ori][0].y+(m_pPic->DMVAP)[Ori][0].y_half
+2*(m_pPic->DMVAP)[Ori][1].y+(m_pPic->DMVAP)[Ori][1].y_half
+2*(m_pPic->DMVAP)[Ori][2].y+(m_pPic->DMVAP)[Ori][2].y_half
+2*(m_pPic->DMVAP)[Ori][3].y+(m_pPic->DMVAP)[Ori][3].y_half)/TRD+4*vmy;
if (VectorW>=0)
VectorW=VectorW/16*2+MVCHR[VectorW%16];
else
VectorW=VectorW/16*2-MVCHR[-(VectorW%16)];
if (VectorH>=0)
VectorH=VectorH/16*2+MVCHR[VectorH%16];
else
VectorH=VectorH/16*2-MVCHR[-(VectorH%16)];
BCurLoss=BLoss.U+y/2*Width/2+x/2;
LoadCBRefer(PrevInter.U,PredData.CB,y+VectorH,x+VectorW,RefWidth,MB_SIZE/2,m_pPic->StrPrevInt);
BCurLoss=BLoss.V+y/2*Width/2+x/2;
LoadCRRefer(PrevInter.V,PredData.CR,y+VectorH,x+VectorW,RefWidth,MB_SIZE/2,m_pPic->StrPrevInt);
VectorW=(TRB-TRD)*(2*(m_pPic->DMVAP)[Ori][0].x+(m_pPic->DMVAP)[Ori][0].x_half+
2*(m_pPic->DMVAP)[Ori][1].x+(m_pPic->DMVAP)[Ori][1].x_half+
2*(m_pPic->DMVAP)[Ori][2].x+(m_pPic->DMVAP)[Ori][2].x_half+
2*(m_pPic->DMVAP)[Ori][3].x+(m_pPic->DMVAP)[Ori][3].x_half)/TRD+4*vmx;
VectorH=(TRB-TRD)*(2*(m_pPic->DMVAP)[Ori][0].y+(m_pPic->DMVAP)[Ori][0].y_half+
2*(m_pPic->DMVAP)[Ori][1].y+(m_pPic->DMVAP)[Ori][1].y_half+
2*(m_pPic->DMVAP)[Ori][2].y+(m_pPic->DMVAP)[Ori][2].y_half+
2*(m_pPic->DMVAP)[Ori][3].y+(m_pPic->DMVAP)[Ori][3].y_half)/TRD+4*vmy;
if (VectorW>=0)
VectorW=VectorW/16*2+MVCHR[VectorW%16];
else
VectorW=VectorW/16*2-MVCHR[-(VectorW%16)];
if (VectorH>=0)
VectorH=VectorH/16*2+MVCHR[VectorH%16];
else
VectorH=VectorH/16*2-MVCHR[-(VectorH%16)];
MVB.x=VectorW/2;
MVB.x_half=VectorW%2;
MVB.y=VectorH/2;
MVB.y_half=VectorH%2;
pLoss=CurLoss.U+y/2*Width/2+x/2;
ystart=max(0,(-MVB.y*2-MVB.y_half+1)/2);
ystop=min(7,7-(MVB.y*2+MVB.y_half+1)/2);
xstart=max(0,(-MVB.x*2-MVB.x_half+1)/2);
xstop=min(7,7-(MVB.x*2+MVB.x_half+1)/2);
BiDirCBPred(ystart,ystop,xstart,xstop,pLoss,MVB);
BCurLoss=BLoss.U+y/2*Width/2+x/2;
MMXReconBlock(BCurLoss,PredData.CB,BData.CB,Width/2);
pLoss=CurLoss.V+y/2*Width/2+x/2;
BiDirCRPred(ystart,ystop,xstart,xstop,pLoss,MVB);
BCurLoss=BLoss.V+y/2*Width/2+x/2;
MMXReconBlock(BCurLoss,PredData.CR,BData.CR,Width/2);
}
void DH263MB::BiDirLumPred(int ystart,int ystop,int xstart,int xstop,BYTE *CurLossPtr,MotionVector MVB)
{ int i,j,BackPred;
for (i=ystart;i<=ystop;i++)
for (j=xstart;j<=xstop;j++)
{if ((!MVB.x_half)&&(!MVB.y_half))
BackPred=CurLossPtr[(i+MVB.y)*Width+(j+MVB.x)];
else if ((!MVB.x_half)&&(MVB.y_half))
BackPred=(CurLossPtr[(i+MVB.y)*Width+(j+MVB.x)]+
CurLossPtr[(i+MVB.y+MVB.y_half)*Width+(j+MVB.x)])/2;
else if ((MVB.x_half)&&(!MVB.y_half))
BackPred=(CurLossPtr[(i+MVB.y)*Width+(j+MVB.x)]+
CurLossPtr[(i+MVB.y)*Width+(j+MVB.x+MVB.x_half)])/2;
else
BackPred=(CurLossPtr[(i+MVB.y)*Width+(j+MVB.x)]+
CurLossPtr[(i+MVB.y)*Width+(j+MVB.x+MVB.x_half)]+
CurLossPtr[(i+MVB.y+MVB.y_half)*Width+(j+MVB.x)]+
CurLossPtr[(i+MVB.y+MVB.y_half)*Width+(j+MVB.x+MVB.x_half)]+
2)/4;
PredData.lum[i*MB_SIZE+j]=(PredData.lum[i*MB_SIZE+j]+BackPred)/2;
}
}
void DH263MB::BiDirCBPred(int ystart,int ystop,int xstart,int xstop,BYTE *CurLossPtr,MotionVector MVB)
{ int BackPred,i,j;
for (i=ystart;i<=ystop;i++)
for (j=xstart;j<=xstop;j++)
{if ((!MVB.x_half)&&(!MVB.y_half))
BackPred=CurLossPtr[(i+MVB.y)*Width/2+(j+MVB.x)];
else if ((!MVB.x_half)&&(MVB.y_half))
BackPred=(CurLossPtr[(i+MVB.y)*Width/2+(j+MVB.x)]+
CurLossPtr[(i+MVB.y+MVB.y_half)*Width/2+(j+MVB.x)])/2;
else if ((MVB.x_half)&&(!MVB.y_half))
BackPred=(CurLossPtr[(i+MVB.y)*Width/2+(j+MVB.x)]+
CurLossPtr[(i+MVB.y)*Width/2+(j+MVB.x+MVB.x_half)])/2;
else
BackPred=(CurLossPtr[(i+MVB.y)*Width/2+(j+MVB.x)]+
CurLossPtr[(i+MVB.y)*Width/2+(j+MVB.x+MVB.x_half)]+
CurLossPtr[(i+MVB.y+MVB.y_half)*Width/2+(j+MVB.x)]+
CurLossPtr[(i+MVB.y+MVB.y_half)*Width/2+(j+MVB.x+MVB.x_half)]+
2)/4;
PredData.CB[i*MB_SIZE/2+j]=(PredData.CB[i*MB_SIZE/2+j]+BackPred)/2;
}
}
void DH263MB::BiDirCRPred(int ystart,int ystop,int xstart,int xstop,BYTE *CurLossPtr,MotionVector MVB)
{ int BackPred,i,j;
for (i=ystart;i<=ystop;i++)
for (j=xstart;j<=xstop;j++)
{if ((!MVB.x_half)&&(!MVB.y_half))
BackPred=CurLossPtr[(i+MVB.y)*Width/2+(j+MVB.x)];
else if ((!MVB.x_half)&&(MVB.y_half))
BackPred=(CurLossPtr[(i+MVB.y)*Width/2+(j+MVB.x)]+
CurLossPtr[(i+MVB.y+MVB.y_half)*Width/2+(j+MVB.x)])/2;
else if ((MVB.x_half)&&(!MVB.y_half))
BackPred=(CurLossPtr[(i+MVB.y)*Width/2+(j+MVB.x)]+
CurLossPtr[(i+MVB.y)*Width/2+(j+MVB.x+MVB.x_half)])/2;
else
BackPred=(CurLossPtr[(i+MVB.y)*Width/2+(j+MVB.x)]+
CurLossPtr[(i+MVB.y)*Width/2+(j+MVB.x+MVB.x_half)]+
CurLossPtr[(i+MVB.y+MVB.y_half)*Width/2+(j+MVB.x)]+
CurLossPtr[(i+MVB.y+MVB.y_half)*Width/2+(j+MVB.x+MVB.x_half)]+
2)/4;
PredData.CR[i*MB_SIZE/2+j]=(PredData.CR[i*MB_SIZE/2+j]+BackPred)/2;
}
}
void DH263MB::AP_BiDirLumPred(int ystart,int ystop,int xstart,int xstop,BYTE *CurLossPtr,MotionVector MVB)
{ int i,j,BackPred,k,Ori;
for (i=ystart;i<=ystop;i++)
for (j=xstart;j<=xstop;j++)
{if ((!MVB.x_half)&&(!MVB.y_half))
BackPred=CurLossPtr[(i+MVB.y)*Width+(j+MVB.x)];
else if ((!MVB.x_half)&&(MVB.y_half))
BackPred=(CurLossPtr[(i+MVB.y)*Width+(j+MVB.x)]+
CurLossPtr[(i+MVB.y+MVB.y_half)*Width+(j+MVB.x)])/2;
else if ((MVB.x_half)&&(!MVB.y_half))
BackPred=(CurLossPtr[(i+MVB.y)*Width+(j+MVB.x)]+
CurLossPtr[(i+MVB.y)*Width+(j+MVB.x+MVB.x_half)])/2;
else
BackPred=(CurLossPtr[(i+MVB.y)*Width+(j+MVB.x)]+
CurLossPtr[(i+MVB.y)*Width+(j+MVB.x+MVB.x_half)]+
CurLossPtr[(i+MVB.y+MVB.y_half)*Width+(j+MVB.x)]+
CurLossPtr[(i+MVB.y+MVB.y_half)*Width+(j+MVB.x+MVB.x_half)]+
2)/4;
k=i/8*2+j/8;
Ori=(i%8)*MB_SIZE/2+(j%8);
PredData.lum[k*64+Ori]=(PredData.lum[k*64+Ori]+BackPred)/2;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -