📄 newmbinter.cpp
字号:
#include "stdafx.h"
#include "color.h"
#include "sactable.h"
#include "CBitstream.h"
#include "sac.h"
#include "Ch263class.h"
#include "Chuffman.h"
#include "commonvar.h"
#include "commonfunc.h"
extern HuffmanCode VLCOD;
extern int *QuantClip;
extern "C"
{
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);
void MMXPredMB(short *Data,BYTE *Select,BYTE *Source,int pixel);
void MMXPredBlock(short *Data,BYTE *Select,BYTE *Source,int pixel);
void MMXPredAPBlock(short *Data,BYTE *Select,BYTE *Source,int pixel);
}
void CH263MB::Predict_P(YUVData Cur,YUVData Inter, int xpos, int ypos)
{ int RefWidth,Ori,pixel=Width/2;
BYTE *SelectPtr;
int VectorH,VectorW;
if (Pic.UMVMode)
RefWidth=Width+32;
else
RefWidth=Width;
Ori=ypos/MB_SIZE*WMB+xpos/MB_SIZE;
SelectPtr=Cur.Y+ypos*Width+xpos;
VectorW=2*(m_pPic->MV)[Ori].x+(m_pPic->MV)[Ori].x_half;
VectorH=2*(m_pPic->MV)[Ori].y+(m_pPic->MV)[Ori].y_half;
LoadRefer(Inter.Y,PredData.lum,ypos*2+VectorH,xpos*2+VectorW,2*RefWidth,MB_SIZE,(m_pPic->StrPrevInt));
MMXPredMB(MBData.lum,SelectPtr,PredData.lum,Width);
if ((m_pPic->MV)[Ori].x_half)
{if (VectorW>=0)
VectorW=(m_pPic->MV)[Ori].x+1-(m_pPic->MV)[Ori].x%2;
else
VectorW=(m_pPic->MV)[Ori].x-1-(m_pPic->MV)[Ori].x%2;
}
else
VectorW=(m_pPic->MV)[Ori].x;
if ((m_pPic->MV)[Ori].y_half)
{if (VectorH>=0)
VectorH=(m_pPic->MV)[Ori].y+1-(m_pPic->MV)[Ori].y%2;
else
VectorH=(m_pPic->MV)[Ori].y-1-(m_pPic->MV)[Ori].y%2;
}
else
VectorH=(m_pPic->MV)[Ori].y;
SelectPtr=Cur.U+ypos/2*Width/2+xpos/2;
LoadCBRefer(Inter.U,PredData.CB,ypos+VectorH,xpos+VectorW,RefWidth,MB_SIZE/2,(m_pPic->StrPrevInt));
MMXPredBlock(MBData.CB,SelectPtr,PredData.CB,Width/2);
SelectPtr=Cur.V+ypos/2*Width/2+xpos/2;
LoadCRRefer(Inter.V,PredData.CR,ypos+VectorH,xpos+VectorW,RefWidth,MB_SIZE/2,(m_pPic->StrPrevInt));
MMXPredBlock(MBData.CR,SelectPtr,PredData.CR,Width/2);
}
void CH263MB::MB_Recon_P(YUVData Loss,int xpos, int ypos)
{ register int pixel=Width/2;
BYTE *Select;
Select=Loss.Y+ypos*Width+xpos;
MMXReconMB(Select,PredData.lum,MBData.lum,Width);
Select=Loss.U+ypos/2*Width/2+xpos/2;
MMXReconBlock(Select,PredData.CB,MBData.CB,Width/2);
Select=Loss.V+ypos/2*Width/2+xpos/2;
MMXReconBlock(Select,PredData.CR,MBData.CR,Width/2);
}
void CH263MB::Predict_P4V(YUVData Cur,YUVData Inter, int xpos, int ypos)
{ int RefWidth,k,Ori;
BYTE *SelectCur;
int VectorH,VectorW;
int MVCHR[16]={0,0,0,1,1,1,1,1,1,1,1,1,1,1,2,2};
if (Pic.UMVMode)
RefWidth=Width+32;
else
RefWidth=Width;
Ori=ypos/MB_SIZE*WMB+xpos/MB_SIZE;
for (k=0;k<4;k++)
{SelectCur=Cur.Y+(ypos+k/2*MB_SIZE/2)*Width+xpos+k%2*MB_SIZE/2;
VectorW=2*(m_pPic->MVAP)[Ori][k].x+(m_pPic->MVAP)[Ori][k].x_half;
VectorH=2*(m_pPic->MVAP)[Ori][k].y+(m_pPic->MVAP)[Ori][k].y_half;
LoadRefer(Inter.Y,PredData.lum+k*64,(ypos+k/2*MB_SIZE/2)*2+VectorH,
(xpos+k%2*MB_SIZE/2)*2+VectorW,2*RefWidth,MB_SIZE/2,(m_pPic->StrPrevInt));
MMXPredAPBlock(MBData.lum+k/2*128+k%2*8,SelectCur,PredData.lum+64*k,Width);
}
VectorW=2*(m_pPic->MVAP)[Ori][0].x+(m_pPic->MVAP)[Ori][0].x_half
+2*(m_pPic->MVAP)[Ori][1].x+(m_pPic->MVAP)[Ori][1].x_half
+2*(m_pPic->MVAP)[Ori][2].x+(m_pPic->MVAP)[Ori][2].x_half
+2*(m_pPic->MVAP)[Ori][3].x+(m_pPic->MVAP)[Ori][3].x_half;
VectorH=2*(m_pPic->MVAP)[Ori][0].y+(m_pPic->MVAP)[Ori][0].y_half
+2*(m_pPic->MVAP)[Ori][1].y+(m_pPic->MVAP)[Ori][1].y_half
+2*(m_pPic->MVAP)[Ori][2].y+(m_pPic->MVAP)[Ori][2].y_half
+2*(m_pPic->MVAP)[Ori][3].y+(m_pPic->MVAP)[Ori][3].y_half;
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)];
SelectCur=Cur.U+ypos/2*Width/2+xpos/2;
LoadCBRefer(Inter.U,PredData.CB,ypos+VectorH,xpos+VectorW,RefWidth,MB_SIZE/2,(m_pPic->StrPrevInt));
MMXPredBlock(MBData.CB,SelectCur,PredData.CB,Width/2);
SelectCur=Cur.V+ypos/2*Width/2+xpos/2;
LoadCRRefer(Inter.V,PredData.CR,ypos+VectorH,xpos+VectorW,RefWidth,MB_SIZE/2,(m_pPic->StrPrevInt));
MMXPredBlock(MBData.CR,SelectCur,PredData.CR,Width/2);
}
void CH263MB::MB_Recon_P4V(YUVData Loss,int xpos, int ypos)
{ int k;
BYTE *SelectPtr;
for (k=0;k<4;k++)
{SelectPtr=Loss.Y+(ypos+k/2*MB_SIZE/2)*Width+xpos+k%2*MB_SIZE/2;
MMXReconAPBlock(SelectPtr,PredData.lum+k*64,
MBData.lum+k/2*128+k%2*8,Width);
}
SelectPtr=Loss.U+ypos/2*Width/2+xpos/2;
MMXReconBlock(SelectPtr,PredData.CB,MBData.CB,Width/2);
SelectPtr=Loss.V+ypos/2*Width/2+xpos/2;
MMXReconBlock(SelectPtr,PredData.CR,MBData.CR,Width/2);
}
int CH263MB::EncodeVectors(int x,int y,CBitStream *OutputStream)
{ int y_vec, x_vec,CodeWord;
int pmvx, pmvy,bitcount=0;
int CLength;
if (x==0)
{pmvx=0;
pmvy=0;
}
else
{pmvx=2*(m_pPic->MV)[y*WMB+x-1].x+(m_pPic->MV)[y*WMB+x-1].x_half;
pmvy=2*(m_pPic->MV)[y*WMB+x-1].y+(m_pPic->MV)[y*WMB+x-1].y_half;
}
x_vec=2*(m_pPic->MV)[y*WMB+x].x+(m_pPic->MV)[y*WMB+x].x_half-pmvx;
y_vec=2*(m_pPic->MV)[y*WMB+x].y+(m_pPic->MV)[y*WMB+x].y_half-pmvy;
if (x_vec < -32)
x_vec += 64;
else if (x_vec > 31)
x_vec -= 64;
if (y_vec < -32)
y_vec += 64;
else if (y_vec > 31)
y_vec -= 64;
if (x_vec < 0)
x_vec += 64;
if (y_vec < 0)
y_vec += 64;
CLength=VLCOD.Encode(x_vec,VLCOD.vlc_mv,CodeWord);
OutputStream->PutVarible(CodeWord,CLength);
bitcount+=CLength;
CLength=VLCOD.Encode(y_vec,VLCOD.vlc_mv,CodeWord);
OutputStream->PutVarible(CodeWord,CLength);
bitcount+=CLength;
return bitcount;
}
int CH263MB::SACEncodeVectors(int x, int y,SAC *SACode)
{ int y_vec, x_vec,bitcount=0;
int pmvx, pmvy;
if (x==0)
{pmvx=0;
pmvy=0;
}
else
{pmvx=2*(m_pPic->MV)[y*WMB+x-1].x+(m_pPic->MV)[y*WMB+x-1].x_half;
pmvy=2*(m_pPic->MV)[y*WMB+x-1].y+(m_pPic->MV)[y*WMB+x-1].y_half;
}
x_vec=2*(m_pPic->MV)[y*WMB+x].x+(m_pPic->MV)[y*WMB+x].x_half-pmvx;
y_vec=2*(m_pPic->MV)[y*WMB+x].y+(m_pPic->MV)[y*WMB+x].y_half-pmvy;
if (x_vec < -32)
x_vec += 64;
else if (x_vec > 31)
x_vec -= 64;
if (y_vec < -32)
y_vec += 64;
else if (y_vec > 31)
y_vec -= 64;
if (x_vec < 0)
x_vec += 64;
if (y_vec < 0)
y_vec += 64;
bitcount+=SACode->AR_Encode(SACode->IndexFN(x_vec,mvdtab,64),cumf_MVD);
bitcount+=SACode->AR_Encode(SACode->IndexFN(y_vec,mvdtab,64),cumf_MVD);
return bitcount;
}
int CH263MB::EncodeAPVectors(int x,int y,CBitStream *OutputStream)
{ int y_vec, x_vec,CodeWord;
int pmvx[4],pmvy[4],bitcount=0;
int block,CLength,start,stop,oo;
if (Mode==MODE_INTER4V)
{start=1;
stop=4;
}
else
{start=0;
stop=0;
}
FindAPPMV(x,y,pmvx,pmvy);
for (block = start; block <= stop; block++)
{if (block>0)
oo=block-1;
else
oo=block;
x_vec=2*(m_pPic->MVAP)[y*WMB+x][oo].x+(m_pPic->MVAP)[y*WMB+x][oo].x_half-pmvx[oo];
y_vec=2*(m_pPic->MVAP)[y*WMB+x][oo].y+(m_pPic->MVAP)[y*WMB+x][oo].y_half-pmvy[oo];
if (x_vec < -32)
x_vec += 64;
else if (x_vec > 31)
x_vec -= 64;
if (y_vec < -32)
y_vec += 64;
else if (y_vec > 31)
y_vec -= 64;
if (x_vec < 0)
x_vec += 64;
if (y_vec < 0)
y_vec += 64;
CLength=VLCOD.Encode(x_vec,VLCOD.vlc_mv,CodeWord);
OutputStream->PutVarible(CodeWord,CLength);
bitcount+=CLength;
CLength=VLCOD.Encode(y_vec,VLCOD.vlc_mv,CodeWord);
OutputStream->PutVarible(CodeWord,CLength);
bitcount+=CLength;
}
return bitcount;
}
int CH263MB::SACEncodeAPVectors(int x, int y,SAC *SACode)
{ int y_vec, x_vec;
int pmvx[4],pmvy[4],bitcount=0;
int block,start,stop,oo;
if (Mode==MODE_INTER4V)
{start=1;
stop=4;
}
else
{start=0;
stop=0;
}
FindAPPMV(x,y,pmvx,pmvy);
for (block = start; block <= stop; block++)
{
if (block>0)
oo=block-1;
else
oo=block;
x_vec=2*(m_pPic->MVAP)[y*WMB+x][oo].x+(m_pPic->MVAP)[y*WMB+x][oo].x_half-pmvx[oo];
y_vec=2*(m_pPic->MVAP)[y*WMB+x][oo].y+(m_pPic->MVAP)[y*WMB+x][oo].y_half-pmvy[oo];
if (x_vec < -32)
x_vec += 64;
else if (x_vec > 31)
x_vec -= 64;
if (y_vec < -32)
y_vec += 64;
else if (y_vec > 31)
y_vec -= 64;
if (x_vec < 0)
x_vec += 64;
if (y_vec < 0)
y_vec += 64;
bitcount+=SACode->AR_Encode(SACode->IndexFN(x_vec,mvdtab,64),cumf_MVD);
bitcount+=SACode->AR_Encode(SACode->IndexFN(y_vec,mvdtab,64),cumf_MVD);
}
return bitcount;
}
int CH263MB::EncodePBVectors(int x,int y,CBitStream *OutputStream)
{ int x_vec,y_vec,bitcount=0;
int CodeWord,CLength;
if (MBInfo.MODB == PBMODE_MVDB || MBInfo.MODB == PBMODE_CBPB_MVDB)
{x_vec=2*(m_pPic->MVPB)[y*WMB+x].x+(m_pPic->MVPB)[y*WMB+x].x_half;
y_vec=2*(m_pPic->MVPB)[y*WMB+x].y+(m_pPic->MVPB)[y*WMB+x].y_half;
if (x_vec < -32)
x_vec += 64;
else if (x_vec > 31)
x_vec -= 64;
if (y_vec < -32)
y_vec += 64;
else if (y_vec > 31)
y_vec -= 64;
if (x_vec < 0)
x_vec += 64;
if (y_vec < 0)
y_vec += 64;
CLength=VLCOD.Encode(x_vec,VLCOD.vlc_mv,CodeWord);
OutputStream->PutVarible(CodeWord,CLength);
bitcount+=CLength;
CLength=VLCOD.Encode(y_vec,VLCOD.vlc_mv,CodeWord);
OutputStream->PutVarible(CodeWord,CLength);
bitcount+=CLength;
}
return bitcount;
}
int CH263MB::SACEncodePBVectors(int x, int y,SAC *SACode)
{ int x_vec,y_vec,bitcount=0;
if (MBInfo.MODB == PBMODE_MVDB || MBInfo.MODB == PBMODE_CBPB_MVDB)
{x_vec=2*(m_pPic->MVPB)[y*WMB+x].x+(m_pPic->MVPB)[y*WMB+x].x_half;
y_vec=2*(m_pPic->MVPB)[y*WMB+x].y+(m_pPic->MVPB)[y*WMB+x].y_half;
if (x_vec < -32)
x_vec += 64;
else if (x_vec > 31)
x_vec -= 64;
if (y_vec < -32)
y_vec += 64;
else if (y_vec > 31)
y_vec -= 64;
if (x_vec < 0)
x_vec += 64;
if (y_vec < 0)
y_vec += 64;
bitcount+=SACode->AR_Encode(SACode->IndexFN(x_vec,mvdtab,64),cumf_MVD);
bitcount+=SACode->AR_Encode(SACode->IndexFN(y_vec,mvdtab,64),cumf_MVD);
}
return bitcount;
}
void CH263MB::FindAPPMV(int x,int y,int pmvx[4],int pmvy[4])
{ int p1x,p2x,p3x,p1y,p2y,p3y;
if (x==0)
{pmvx[0]=0;
pmvy[0]=0;
}
else
{pmvx[0]=2*(m_pPic->MVAP)[y*WMB+x-1][1].x+(m_pPic->MVAP)[y*WMB+x-1][1].x_half;
pmvy[0]=2*(m_pPic->MVAP)[y*WMB+x-1][1].y+(m_pPic->MVAP)[y*WMB+x-1][1].y_half;
}
pmvx[1]=2*(m_pPic->MVAP)[y*WMB+x][0].x+(m_pPic->MVAP)[y*WMB+x][0].x_half;
pmvy[1]=2*(m_pPic->MVAP)[y*WMB+x][0].y+(m_pPic->MVAP)[y*WMB+x][0].y_half;
if (x==0)
{p1x=0;
p1y=0;
}
else
{p1x=2*(m_pPic->MVAP)[y*WMB+x-1][3].x+(m_pPic->MVAP)[y*WMB+x-1][3].x_half;
p1y=2*(m_pPic->MVAP)[y*WMB+x-1][3].y+(m_pPic->MVAP)[y*WMB+x-1][3].y_half;
}
p2x=2*(m_pPic->MVAP)[y*WMB+x][0].x+(m_pPic->MVAP)[y*WMB+x][0].x_half;
p2y=2*(m_pPic->MVAP)[y*WMB+x][0].y+(m_pPic->MVAP)[y*WMB+x][0].y_half;
p3x=2*(m_pPic->MVAP)[y*WMB+x][1].x+(m_pPic->MVAP)[y*WMB+x][1].x_half;
p3y=2*(m_pPic->MVAP)[y*WMB+x][1].y+(m_pPic->MVAP)[y*WMB+x][1].y_half;
pmvx[2]=p1x+p2x+p3x-max(p1x,max(p2x,p3x))-min(p1x,min(p2x,p3x));
pmvy[2]=p1y+p2y+p3y-max(p1y,max(p2y,p3y))-min(p1y,min(p2y,p3y));
p1x=2*(m_pPic->MVAP)[y*WMB+x][2].x+(m_pPic->MVAP)[y*WMB+x][2].x_half;
p1y=2*(m_pPic->MVAP)[y*WMB+x][2].y+(m_pPic->MVAP)[y*WMB+x][2].y_half;
p2x=2*(m_pPic->MVAP)[y*WMB+x][0].x+(m_pPic->MVAP)[y*WMB+x][0].x_half;
p2y=2*(m_pPic->MVAP)[y*WMB+x][0].y+(m_pPic->MVAP)[y*WMB+x][0].y_half;
p3x=2*(m_pPic->MVAP)[y*WMB+x][1].x+(m_pPic->MVAP)[y*WMB+x][1].x_half;
p3y=2*(m_pPic->MVAP)[y*WMB+x][1].y+(m_pPic->MVAP)[y*WMB+x][1].y_half;
pmvx[3]=p1x+p2x+p3x-max(p1x,max(p2x,p3x))-min(p1x,min(p2x,p3x));
pmvy[3]=p1y+p2y+p3y-max(p1y,max(p2y,p3y))-min(p1y,min(p2y,p3y));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -