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 CLAYERPARSER_H
00049 #define CLAYERPARSER_H
00050
00051 #include "datatype.h"
00052 #include "mpegdata.h"
00053 #include "cstrmbuf.h"
00054 #include "mpvdata.h"
00055
00056 const GOPHEADER DEFAULT_GOP_HEADER={0,0,0,0,0,0,0};
00057
00058 class CLayerParser
00059 {
00060
00061 public:
00062 CLayerParser();
00063 ~CLayerParser();
00064
00065
00066 protected:
00067 FILE *pfFile;
00068 CStreamBuf StreamBuf;
00069 HEADERDATA HeaderData;
00070
00071 long lMaxFrameNum;
00072 long lMinFrameNum;
00073 long lAvgPictureScale;
00074 long lMinPictureSize;
00075 double dfFrameRate;
00076
00077 bool bMpeg2;
00078
00079 VIDEOCHNUM VideoChNum;
00080 PICTUREINFO CurPicInfo;
00081 int iParserStatus;
00082
00083
00084 public:
00085 int Initialize(const char *psFileName, VIDEOCHNUM ChannelNum, DWORD dwBufSize=VIDEO_STREAM_BUF_SIZE);
00086 int Initialize(const char *psFileName, bool bIsSystemStream, VIDEOCHNUM ChannelNum, DWORD dwBufSize=VIDEO_STREAM_BUF_SIZE);
00087 void Reset(void);
00088 int PrepareRandomAccess(void);
00089 int GetStatus(void) { return iParserStatus; };
00090 inline void ClearStatus(void);
00091
00092 int MoveToNextPicture(void);
00093 int MoveToPreviousPicture(void);
00094 int ResetCurPicture(void);
00095 int MoveToNextFrame(void);
00096 int MoveToPreviousFrame(void);
00097 int ResetCurFrame(void);
00098
00099 int JumpToHeadingPicture(void);
00100 int JumpToEndingPicture(void);
00101 int JumpToFrame(DWORD dwFrameNum);
00102 int UpdateGOPInfo(void);
00103 inline int GetCurFrameNum(long *plCurFrameNum);
00104 inline int GetMaxFrameNum(long *plMaxFrameNum);
00105
00106 inline void RecordCurPicture(PICTURERECORD *pPicRecord);
00107 inline int RestorePicture(const PICTURERECORD *pPicRecord);
00108
00109
00110 CStreamBuf *GetLayerParserBuf(void) { return &StreamBuf; };
00111 const HEADERDATA *GetHeaderData(void) { return &HeaderData; };
00112 const PICTUREINFO *GetCurPicInfo(void) { return &CurPicInfo; };
00113
00114 bool IsMpeg2(void) { return bMpeg2; };
00115 inline void GetProfileLevel(short *pshProfile, short *pshLevel);
00116
00117 inline int SetMinPicSize(long lNewMinPicSize);
00118
00119 protected:
00120 void NextStartCode(void);
00121 void PreviousStartCode(void);
00122 void SetDefaultStatus(void);
00123 inline void MarkerBit(void);
00124 int TestStream(bool *pbIsSystemStream);
00125
00126 int ReadSequenceHeader(void);
00127 int ReadGOPHeader(void);
00128 int ReadExtensionAndUserDataV(void);
00129 int ReadSequenceExtension(void);
00130 int ReadSequenceDisplayExtension(void);
00131 int ReadSequenceScalableExtension(void);
00132
00133 int ReadPictureHeader(void);
00134 int ReadExtensionAndUserDataP(void);
00135 int ReadQuantMatrixExtension(void);
00136 int ReadPictureExtension(void);
00137 int ReadPictureDisplayExtension(void);
00138 int ReadPictureSpatialScalableExtension(void);
00139 int ReadPictureTemporalScalableExtension(void);
00140 int ReadCopyrightExtension(void);
00141 inline void ReadExtraBitInfo(void);
00142 inline void ReadUserData(void);
00143
00144 int GetFrameNum(long *plCurFrameNum);
00145 int RestorePicture(const PACKETPOS &PictureBasePckPos, long lPictureBasePckOffset, char cFieldValue=-1);
00146 int LocateToNextPicture(char cDefaultField, bool bDefaultInvalidGOP, long lDefaultUnwrappedNum);
00147 void LocateToNextPicUpdate(bool bGOPChanged, char cDefaultField,
00148 bool bDefaultInvalidGOP, long lDefaultUnwrappedNum);
00149 int LocateCurrentPicture(char cDefaultField, bool bInvalidGOP, long lUnwrappedNum);
00150 void LocateCurrentPicUpdate(bool bGOPChanged, char cDefaultField,
00151 bool bInvalidGOP, long lUnwrappedNum);
00152 void MoveToNextPicUpdate(bool bGOPChanged, BYTE nPreviousPicStructure);
00153 void MoveToPreviousPicUpdate(bool bGOPChanged);
00154 int CurFrameGOPUpdate(void);
00155 };
00156
00158
00159
00160 inline void CLayerParser::ClearStatus(void)
00161 {
00162 if (iParserStatus!=NOT_USED)
00163 {
00164 iParserStatus=MDC_SUCCESS;
00165
00166 }
00167 }
00168
00169 inline void CLayerParser::GetProfileLevel(short *pshProfile, short *pshLevel)
00170 {
00171
00172 if((HeaderData.SequenceExtension.nProfileAndLevelIndication>>7) & 1)
00173 {
00174
00175 if((HeaderData.SequenceExtension.nProfileAndLevelIndication&15)==5)
00176 {
00177 *pshProfile=PROFILE_422;
00178 *pshLevel=MAIN_LEVEL;
00179 }
00180 else
00181 {
00182 *pshProfile=0;
00183 *pshLevel=0;
00184 }
00185 }
00186 else
00187 {
00188 *pshProfile=HeaderData.SequenceExtension.nProfileAndLevelIndication >> 4;
00189 *pshLevel=HeaderData.SequenceExtension.nProfileAndLevelIndication & 0xF;
00190 }
00191 }
00192
00193
00194 inline int CLayerParser::GetCurFrameNum(long *plCurFrameNum)
00195 {
00196 int iStatus;
00197
00198 StreamBuf.ClearStatus();
00199 iStatus=GetFrameNum(plCurFrameNum);
00200 if (iStatus!=MDC_SUCCESS)
00201 {
00202 return iStatus;
00203 }
00204 *plCurFrameNum-=lMinFrameNum;
00205 if (*plCurFrameNum<0)
00206 {
00207 iParserStatus=STREAM_ERROR;
00208 return UNSUCCESS;
00209 }
00210 return MDC_SUCCESS;
00211 }
00212
00213
00214 inline int CLayerParser::GetMaxFrameNum(long *plMaxFrameNum)
00215 {
00216 int iStatus;
00217
00218 if (lMaxFrameNum<0)
00219 iStatus=PrepareRandomAccess();
00220 *plMaxFrameNum=lMaxFrameNum-lMinFrameNum;
00221 return iStatus;
00222 }
00223
00224
00225 inline void CLayerParser::RecordCurPicture(PICTURERECORD *pPicRecord)
00226 {
00227 pPicRecord->PicInfo=CurPicInfo;
00228 pPicRecord->bGOPValid=HeaderData.bGOPValid;
00229 if (HeaderData.bGOPValid)
00230 pPicRecord->GOPHeader=HeaderData.GOPHeader;
00231 }
00232
00233 inline int CLayerParser::RestorePicture(const PICTURERECORD *pPicRecord)
00234 {
00235 int iStatus;
00236
00237 iStatus=RestorePicture(pPicRecord->PicInfo.CurPicturePckPos,pPicRecord->PicInfo.lCurPicturePckOffset,
00238 pPicRecord->PicInfo.bSecondField);
00239 if (iStatus!=MDC_SUCCESS)
00240 return iStatus;
00241 CurPicInfo.lUnwrappedFrameNum=pPicRecord->PicInfo.lUnwrappedFrameNum;
00242 HeaderData.bGOPValid=pPicRecord->bGOPValid;
00243 if (pPicRecord->bGOPValid)
00244 HeaderData.GOPHeader=pPicRecord->GOPHeader;
00245 return MDC_SUCCESS;
00246 }
00247
00249
00250
00251 inline int CLayerParser::SetMinPicSize(long lNewMinPicSize)
00252 {
00253 if (lNewMinPicSize<MIN_GOP_HEADER_SIZE)
00254 {
00255 iParserStatus=USE_ERROR;
00256 return UNSUCCESS;
00257 }
00258 lMinPictureSize=lNewMinPicSize;
00259 return MDC_SUCCESS;
00260 }
00261
00263
00264
00265 inline void CLayerParser::MarkerBit(void)
00266 {
00267 #ifdef ERR_INFO
00268 if (!StreamBuf.GetByteBits(1))
00269 {
00270 iParserStatus=STREAM_ERROR;
00271 }
00272 #else
00273 StreamBuf.GetByteBits(1);
00274 #endif
00275 }
00276
00277
00278 inline void CLayerParser::ReadExtraBitInfo(void)
00279 {
00280 while (StreamBuf.GetByteBits(1))
00281 StreamBuf.GetByteBits(8);
00282 }
00283
00284
00285 inline void CLayerParser::ReadUserData(void)
00286 {
00287
00288 NextStartCode();
00289 }
00290
00291 #endif //CLAYERPARSE_H