00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00047
00048 #if !defined CPICDECODER_H
00049 #define CPICDECODER_H
00050
00051 #include <memory.h>
00052 #include <string.h>
00053 #include "datatype.h"
00054 #include "mpegdata.h"
00055 #include "cstrmbuf.h"
00056 #include "mpvdata.h"
00057 #include "clayerpa.h"
00058
00059
00060 const BYTE TABLE_6_20[3] = {6,8,12};
00061 const BYTE STWC_TABLE[3][4] = { {6,3,7,4}, {2,1,5,4}, {2,5,7,4} };
00062 const BYTE STWCLASS_TABLE[9] = {0, 1, 2, 1, 1, 2, 3, 3, 4};
00063
00064 class CPicDecoder
00065 {
00066
00067 public:
00068 CPicDecoder();
00069 ~CPicDecoder();
00070
00071
00072 protected:
00073 LAYERTYPE CurLayerType;
00074 DECODERINFO PicDecoderInfo;
00075 CStreamBuf *pStreamBuf[MAX_LAYER_NUM];
00076 const HEADERDATA *pHeaderData[MAX_LAYER_NUM];
00077 const PICTUREINFO *pCurPicInfo[MAX_LAYER_NUM];
00078
00079
00080 BYTE *pnBackRefFrame[3];
00081 BYTE *pnFwdRefFrame[3];
00082 BYTE *pnAuxFrame[3];
00083 BYTE *pnCurFrame[3];
00084
00085 BYTE *pnComTempBuf[3];
00086 DWORD dwCurTempBufSize[3];
00087
00088 short *pshLowerLayerTemp;
00089
00090 WORD wCodedFrameWidth;
00091 WORD wCodedFrameHeight;
00092 WORD wCodedChromaWidth;
00093 WORD wCodedChromaHeight;
00094 bool bMpeg2[MAX_LAYER_NUM];
00095
00096 BYTE nMBBlockNum;
00097 WORD wMBWidth;
00098 WORD wMBHeight;
00099 DWORD dwMBMaxNum;
00100
00101 double dfIDCTCoef[BLOCK_SIZE][BLOCK_SIZE];
00102 short *pshFastIDCTCoef;
00103
00104 BYTE *pnClip;
00105
00106 bool bWarningFlag;
00107
00108 private:
00109 BYTE pnBuf[1024];
00110 short pshFastIDCTBuf[FAST_IDCT_COEF_NUM];
00111
00112 BYTE nSliceVerticalPositionExtension;
00113
00114 BYTE nMBType[MAX_LAYER_NUM];
00115 BYTE nPriorityBreakpoint[MAX_LAYER_NUM];
00116 BYTE nQuantizerScale[MAX_LAYER_NUM];
00117 BYTE nIntraSlice[MAX_LAYER_NUM];
00118 short shBlock[MAX_LAYER_NUM][12][64];
00119
00120 short shPMV[2][2][2];
00121
00122
00123 public:
00124 int Initialize(CLayerParser *pBaseLayerParser, CLayerParser *pEnhanLayerParser);
00125 void Reset(void);
00126 int GetStatus(void) { return PicDecoderInfo.iStatus; };
00127 inline void ClearStatus(void);
00128 inline void SetSpeed(bool bIsHighSpeed) { PicDecoderInfo.bSpeedFlag=bIsHighSpeed; };
00129
00130 int DecodePicture(void);
00131 int DecodePicture(BYTE *pnBaseFrameData[3], WORD wYRowIncr, WORD wChromaRowIncr);
00132
00133 inline void GetCodingPicPara(CODINGPICPARA *pCodingPicPara);
00134 long GetCurFrame(BYTE *pnOutData,WORD *pwOutPicWidth,WORD *pwOutPicHeight,BYTE nOutputType);
00135 long GetCurPic(BYTE *pnOutData,WORD *pwOutPicWidth,WORD *pwOutPicHeight,BYTE nOutputType);
00136 int StoreCurPic(const char *psOutFileName, BYTE nOutputType);
00137 int StoreCurFrame(const char *psOutFileName, BYTE nOutputType);
00138 inline int GetCurYUV(BYTE *pnOutData[3],WORD pwOutPicWidth[3],WORD pwOutPicHeight[3], bool bFrameOnly);
00139 inline int GetCurRGB(BYTE *pnOutData[3],WORD *pwOutPicWidth,WORD *pwOutPicHeight, bool bFrameOnly);
00140
00141
00142 inline void RelocateBuf(bool bSwapRefBuf);
00143 void DataSequential(BYTE *pnSourceData, WORD wDataWidth, WORD wDataHeight,
00144 long lIncreasement, BYTE *pnTargetData);
00145 int HorizontalInterpolation(BYTE *pnSourceData, WORD wDataWidth, WORD wDataHeight,
00146 long lIncreasement, BYTE *pnTargetData, BYTE nWorkMode);
00147 int VerticalInterpolation(BYTE *pnSourceData, WORD wDataWidth, WORD wDataHeight,
00148 long lIncreasement, BYTE *pnTargetData, BYTE nWorkMode);
00149
00150
00151 int GetMBBlockPic(MBBLOCK *pOutMBBlockPic,WORD *pwOutWidth,WORD *pwOutHeight);
00152 int GetCompressedPic(COMPRESSEDPIC *pOutCompressedPic);
00153
00154 protected:
00155 void SetDefaultStatus(void);
00156 int CompareTwoStreamInfo(void);
00157 void NextStartCode(CStreamBuf *pStreamBuf);
00158 inline int PrepareComTempBuf(BYTE nComponent, DWORD dwNewSize);
00159
00160 void InitializeFastIDCT(void);
00161 void FastIDCT(short *pshBlock);
00162 void IDCTRow(short *pshBlock);
00163 void IDCTCol(short *pshBlock);
00164
00165 void InitializeReferenceIDCT(void);
00166 void ReferenceIDCT(short *pshBlock);
00167
00168 int ReadSliceHeader(void);
00169 inline void ReadExtraBitInfo(void);
00170 inline void MarkerBit(void);
00171
00172 void SpatialPrediction(BYTE *pnBaseFrameData[3], WORD wYRowIncr, WORD wChromaRowIncr, BYTE *pnDestFrame[3]);
00173 void MakeSpatialPredictionFrame(BYTE *pSourceFrm, WORD wSourceWidth,
00174 BYTE *pnTempFrm, short *pshTemp, BYTE *pnDestination,
00175 short shHOffset, short shVOffset, WORD wPredWidth,
00176 WORD wPredHeight, WORD wPicWidth, WORD wPicHeight,
00177 BYTE nAperture);
00178 void Deinterlace(BYTE *pnSource, WORD wSourceWidth, BYTE nFieldValue,
00179 BYTE *pnDest, WORD wDestWidth, WORD wDestHeight, BYTE nAperture);
00180 void SubsampleVertical(BYTE *pnSource, WORD wSourceWidth, WORD wSourceHeight,
00181 short *pshDestination, WORD wDestWidth, WORD wDestHeight,
00182 BYTE nFactorM, BYTE nFactorN, BYTE nFieldValue,
00183 BYTE nIncLineNum);
00184 void SubsampleHorizontal(short *pshSource, BYTE *pnDestination, WORD wHOffset,
00185 WORD wWidth, WORD wSourceWidth, WORD wDestWidth,
00186 WORD wHeight, BYTE nFactorM, BYTE nFactorN);
00187
00188 void DecodePictureData(void);
00189 int MoveToNextSlice(DWORD &dwMBA, DWORD &dwMBAInc, short shDctDcPred[3]);
00190 int DecodeSlice(DWORD dwPicMBMaxNum,DWORD dwMBA,DWORD dwMBAInc,short shDctDcPred[3]);
00191 void MacroblockModes(BYTE *pnStwType, BYTE *pnStwClass, char *pcMotionType,
00192 BYTE *pnMotionVectorCount,BYTE *pnMvFormat,bool *pbDmv,bool *pbMvScale,char *pcDctType);
00193 void MacroblockModes(BYTE *pnStwType, char *pcDctType);
00194 void AddBlock(BYTE nComponent,WORD wMBCol,WORD wMBRow,char cDctType,bool bAddFlag);
00195 void DecodeSNRMacroblock(DWORD *pdwSNRMBA,DWORD *pdwSNRMBAInc,DWORD dwMBA,
00196 DWORD dwPicMBMaxNum,char *pcDctType);
00197 inline void ClearBlock(BYTE nComponent);
00198 void SumBlock(BYTE nComponent);
00199 void Saturate(short *pshCurBlock);
00200 void MotionCompensation(DWORD dwMBA,char cMotionType,BYTE nMotionVerticalFieldSelect[2][2],
00201 char cDmVector[2],BYTE nStwType,char cDctType);
00202 void SkipMacroblock(short shDctDcPred[3],char *pcMotionType,
00203 BYTE nMotionVerticalFieldSelect[2][2],BYTE *pnStwType);
00204 int DecodeMacroblock(BYTE *pnStwType, BYTE *pnStwClass, char *pcMotionType,
00205 char *pcDctType,short *shDctDcPred, BYTE nMotionVerticalFieldSelect[2][2],
00206 char cDmVector[2]);
00207
00208
00209 BYTE GetIMBType(void);
00210 BYTE GetPMBType(void);
00211 BYTE GetBMBType(void);
00212 BYTE GetDMBType(void);
00213
00214 BYTE GetISpatialMBType(void);
00215 BYTE GetPSpatialMBType(void);
00216 BYTE GetBSpatialMBType(void);
00217 BYTE GetSNRMBType(void);
00218
00219 void GetMacroblockType(void);
00220 char GetMotionCode(void);
00221 char GetDmVector(void);
00222 BYTE GetCodedBlockPattern(void);
00223 DWORD GetMacroblockAddressIncrement(void);
00224 short GetLumaDctDcDiff(void);
00225 short GetChromaDctDcDiff(void);
00226
00227
00228 void DecodeMPEG1IntraBlock(BYTE nComponent, short shDctDcPred[]);
00229 void DecodeMPEG1NonIntraBlock(BYTE nComponent);
00230 void DecodeMPEG2IntraBlock(BYTE nComponent,short shDctDcPred[]);
00231 void DecodeMPEG2NonIntraBlock(BYTE nComponent);
00232
00233
00234 void DecodeMPEG2MotionVector(char cDmVector[2],BYTE nMotionVerticalFieldSelect[2][2],
00235 BYTE nPredDirection,BYTE nMotionVectorCount,BYTE nMvFormat,BYTE nHorizontalRSize,
00236 BYTE nVerticalRSize,bool bDmv,bool bMvScale);
00237 void DecodeMPEG1MotionVector(short *pshPMVVector, char *pcDmVector, BYTE nHorizontalRSize,
00238 BYTE nVerticalRSize, bool bDmv, bool bMvScale, BYTE nFullPelVector);
00239 void DecodeMotionVector(short *pshPMVValue, BYTE nRSize, char cMotionCode,
00240 WORD wMotionResidual, BYTE nFullPelVector);
00241 void DualPrimeArithmetic(short shDMV[][2],char *pcDmVector,short shHorizontalMV,short shVerticalMV);
00242 void GetPredictionPic(WORD wMBCol, WORD wMBRow, char cMotionType,
00243 BYTE nMotionVerticalFieldSelect[2][2], char cDmVector[2], BYTE nStwType);
00244 void FormPrediction(BYTE *pnRefSource[],BYTE nSourceFieldValue,BYTE *pnDestination[],
00245 BYTE nDstFieldValue,WORD wPicIncreasement,WORD wBlkIncreasement,
00246 BYTE nPredBlkWidth,BYTE nPredBlkHeight,WORD wCol,WORD wRow,short shDH,
00247 short shDV,BYTE nAverageFlag);
00248 void FormComponentPrediction(BYTE *pnRefSource,BYTE *pnDestination,WORD wPicIncreasement,
00249 WORD wBlkIncreasement,BYTE nPredBlkWidth,BYTE nPredBlkHeight,
00250 WORD wCol,WORD wRow,short shDH,short shDV,BYTE nAverageFlag);
00251
00252 long GetBMPPic(BYTE *pnOutData,WORD *pwOutPicWidth,WORD *pwOutPicHeight,
00253 BYTE *pnSourceData[3], bool bFrameOnly=false);
00254 long GetTGAPic(BYTE *pnOutData,WORD *pwOutPicWidth,WORD *pwOutPicHeight,
00255 BYTE *pnSourceData[3], bool bFrameOnly=false);
00256 long GetSIFPic(BYTE *pnOutData,WORD *pwOutPicWidth,WORD *pwOutPicHeight,
00257 BYTE *pnSourceData[3], bool bFrameOnly=false);
00258 long GetPPMPic(BYTE *pnOutData,WORD *pwOutPicWidth,WORD *pwOutPicHeight,
00259 BYTE *pnSourceData[3], bool bFrameOnly=false);
00260 int StoreYUVPic(const char *psOutFileName,BYTE *pnSourceData[3], bool bFrameOnly=false);
00261 int StoreComponent(const char *psOutFileName,BYTE *pnSourceData,WORD wStoredPicWidth,
00262 WORD wStoredPicHeight,long lIncreasement);
00263 int GetYUVPic(BYTE *pnOutData[3],WORD pwOutPicWidth[3],WORD pwOutPicHeight[3],
00264 BYTE *pnSourceData[3], bool bFrameOnly=false);
00265 int GetRGBPic(BYTE *pnOutData[3],WORD *pwOutPicWidth,WORD *pwOutPicHeight,
00266 BYTE *pnSourceData[3], bool bFrameOnly=false);
00267
00268 int GetMBBlock(DWORD dwPicMBMaxNum,DWORD dwMBA,DWORD dwMBAInc,short shDctDcPred[3],
00269 MBBLOCK *pOutMBBlockPic);
00270 int GetCompressedBlock(DWORD dwPicMBMaxNum,DWORD dwMBA,DWORD dwMBAInc,short shDctDcPred[3],
00271 COMPRESSEDPIC *pOutCompressedPic);
00272
00273
00274 public:
00275 inline BYTE * GetCurFrameBuf(BYTE nComponent);
00276 inline BYTE *GetBackRefFrameBuf(BYTE nComponent);
00277 inline BYTE *GetFwdRefFrameBuf(BYTE nComponent);
00278 };
00279
00281
00282
00283 inline void CPicDecoder::ClearStatus(void)
00284 {
00285 if (PicDecoderInfo.iStatus!=NOT_USED)
00286 PicDecoderInfo.iStatus=MDC_SUCCESS;
00287 }
00288
00289 inline void CPicDecoder::GetCodingPicPara(CODINGPICPARA *pCodingPicPara)
00290 {
00291 pCodingPicPara->wCodedFrameWidth=wCodedFrameWidth;
00292 pCodingPicPara->wCodedFrameHeight=wCodedFrameHeight;
00293 pCodingPicPara->wCodedChromaWidth=wCodedChromaWidth;
00294 pCodingPicPara->wCodedChromaHeight=wCodedChromaHeight;
00295 pCodingPicPara->wMBWidth=wMBWidth;
00296 pCodingPicPara->wMBHeight=wMBHeight;
00297 }
00298
00299 inline int CPicDecoder::GetCurYUV(BYTE *pnOutData[3],WORD pwOutPicWidth[3],WORD pwOutPicHeight[3], bool bFrameOnly)
00300 {
00301 BYTE *pnCurDataBuf[3];
00302
00303 if (bFrameOnly)
00304 {
00305 pnCurDataBuf[0]=GetCurFrameBuf(0);
00306 pnCurDataBuf[1]=GetCurFrameBuf(1);
00307 pnCurDataBuf[2]=GetCurFrameBuf(2);
00308 return GetYUVPic(pnOutData,pwOutPicWidth,pwOutPicHeight,pnCurDataBuf,true);
00309 }
00310 else
00311 return GetYUVPic(pnOutData,pwOutPicWidth,pwOutPicHeight,pnCurFrame);
00312 }
00313
00314 inline int CPicDecoder::GetCurRGB(BYTE *pnOutData[3],WORD *pwOutPicWidth,WORD *pwOutPicHeight, bool bFrameOnly)
00315 {
00316 BYTE *pnCurDataBuf[3];
00317
00318 if (bFrameOnly)
00319 {
00320 pnCurDataBuf[0]=GetCurFrameBuf(0);
00321 pnCurDataBuf[1]=GetCurFrameBuf(1);
00322 pnCurDataBuf[2]=GetCurFrameBuf(2);
00323 return GetRGBPic(pnOutData,pwOutPicWidth,pwOutPicHeight,pnCurDataBuf,true);
00324 }
00325 else
00326 return GetRGBPic(pnOutData,pwOutPicWidth,pwOutPicHeight,pnCurFrame);
00327 }
00328
00329
00330
00331 inline void CPicDecoder::RelocateBuf(bool bSwapRefBuf)
00332 {
00333 BYTE nCount;
00334 BYTE *pnBufTemp;
00335
00336 if (bSwapRefBuf)
00337 {
00338 for (nCount=0; nCount<3; nCount++)
00339 {
00340 pnBufTemp=pnFwdRefFrame[nCount];
00341 pnFwdRefFrame[nCount]=pnBackRefFrame[nCount];
00342 pnBackRefFrame[nCount]=pnBufTemp;
00343 }
00344 }
00345
00346 if (pHeaderData[BASELAYER]->PictureHeader.nPictureCodingType==B_TYPE)
00347 {
00348 for (nCount=0; nCount<3; nCount++)
00349 pnCurFrame[nCount]=pnAuxFrame[nCount];
00350 }
00351 else
00352 {
00353 for (nCount=0; nCount<3; nCount++)
00354 pnCurFrame[nCount]=pnBackRefFrame[nCount];
00355 }
00356 if (pHeaderData[BASELAYER]->PictureExtension.nPictureStructure==BOTTOM_FIELD)
00357 {
00358 pnCurFrame[0]+=wCodedFrameWidth;
00359 pnCurFrame[1]+=wCodedChromaWidth;
00360 pnCurFrame[2]+=wCodedChromaWidth;
00361 }
00362 }
00363
00364
00365 inline BYTE *CPicDecoder::GetCurFrameBuf(BYTE nComponent)
00366 {
00367 if (nComponent>=3)
00368 {
00369 PicDecoderInfo.iStatus=USE_ERROR;
00370 return NULL;
00371 }
00372 if (pHeaderData[BASELAYER]->PictureHeader.nPictureCodingType==B_TYPE)
00373 return pnAuxFrame[nComponent];
00374 else
00375 return pnBackRefFrame[nComponent];
00376 }
00377
00378 inline BYTE *CPicDecoder::GetBackRefFrameBuf(BYTE nComponent)
00379 {
00380 if (nComponent>=3)
00381 {
00382 PicDecoderInfo.iStatus=USE_ERROR;
00383 return NULL;
00384 }
00385 return pnBackRefFrame[nComponent];
00386 }
00387
00388 inline BYTE *CPicDecoder::GetFwdRefFrameBuf(BYTE nComponent)
00389 {
00390 if (nComponent>=3)
00391 {
00392 PicDecoderInfo.iStatus=USE_ERROR;
00393 return NULL;
00394 }
00395 return pnFwdRefFrame[nComponent];
00396 }
00397
00399
00400
00401
00402 inline void CPicDecoder::ReadExtraBitInfo(void)
00403 {
00404 while (pStreamBuf[CurLayerType]->GetByteBits(1))
00405 pStreamBuf[CurLayerType]->GetByteBits(8);
00406 }
00407
00408 inline void CPicDecoder::MarkerBit(void)
00409 {
00410 #ifdef ERR_INFO
00411 if (!pStreamBuf[CurLayerType]->GetByteBits(1))
00412 {
00413 PicDecoderInfo.iStatus=STREAM_ERROR;
00414 }
00415 #else
00416 pStreamBuf[CurLayerType]->GetByteBits(1);
00417 #endif
00418 }
00419
00420
00421 inline void CPicDecoder::ClearBlock(BYTE nComponent)
00422 {
00423 memset(shBlock[CurLayerType][nComponent],0,64*sizeof(short));
00424 }
00425
00426
00427 inline int CPicDecoder::PrepareComTempBuf(BYTE nComponent, DWORD dwNewSize)
00428 {
00429 #ifdef DEBUG
00430 if (nComponent>=3)
00431 {
00432 PicDecoderInfo.iStatus=USE_ERROR;
00433 return UNSUCCESS;
00434 }
00435 #endif
00436 if (dwCurTempBufSize[nComponent]<dwNewSize)
00437 {
00438 if (pnComTempBuf[nComponent]!=NULL)
00439 delete [] pnComTempBuf[nComponent];
00440 pnComTempBuf[nComponent]=new BYTE [dwNewSize];
00441 if (pnComTempBuf[nComponent]==NULL)
00442 {
00443 dwCurTempBufSize[nComponent]=0;
00444 PicDecoderInfo.iStatus=MEM_ERROR;
00445 return UNSUCCESS;
00446 }
00447 dwCurTempBufSize[nComponent]=dwNewSize;
00448 }
00449 return MDC_SUCCESS;
00450 }
00451
00452 #endif //CPICDECODER_H