OggPageHeader.cpp

Go to the documentation of this file.
00001 //===========================================================================
00002 //Copyright (C) 2003, 2004 Zentaro Kavanagh
00003 //
00004 //Redistribution and use in source and binary forms, with or without
00005 //modification, are permitted provided that the following conditions
00006 //are met:
00007 //
00008 //- Redistributions of source code must retain the above copyright
00009 //  notice, this list of conditions and the following disclaimer.
00010 //
00011 //- Redistributions in binary form must reproduce the above copyright
00012 //  notice, this list of conditions and the following disclaimer in the
00013 //  documentation and/or other materials provided with the distribution.
00014 //
00015 //- Neither the name of Zentaro Kavanagh nor the names of contributors 
00016 //  may be used to endorse or promote products derived from this software 
00017 //  without specific prior written permission.
00018 //
00019 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 //``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00022 //PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
00023 //CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00024 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00025 //PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00026 //PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00027 //LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028 //NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 //===========================================================================
00031 
00032 #include "stdafx.h"
00033 #include <libOOOgg/OggPageHeader.h>
00034 
00035 
00036 OggPageHeader::OggPageHeader(void)
00037         :       mPageSize(0)
00038         ,       mHeaderSize(0)
00039         ,       mDataSize(0)
00040         ,       mStructureVersion(0)
00041         ,       mHeaderFlags(0)
00042         ,       mStreamSerialNo(0)
00043         ,       mPageSequenceNo(0)
00044         ,       mCRCChecksum(0)
00045         ,       mPageState(BLANK)
00046         ,       mGranulePos(0)
00047         ,       mSegmentTable(NULL)
00048         ,       mNumPageSegments(0)
00049                 
00050 {
00051 
00052 }
00053 
00054 OggPageHeader::~OggPageHeader(void)
00055 {
00056         delete[] mSegmentTable;
00057 }
00058 
00059 //Gives a pointer to the caller.
00060 OggPageHeader* OggPageHeader::clone() {
00061 
00062         OggPageHeader* retClone = new OggPageHeader();
00063         retClone->mCRCChecksum = mCRCChecksum;
00064         retClone->mDataSize = mDataSize;
00065         retClone->mGranulePos = mGranulePos;
00066         retClone->mHeaderFlags = mHeaderFlags;
00067         retClone->mHeaderSize = mHeaderSize;
00068         retClone->mNumPageSegments = mNumPageSegments;
00069         retClone->mPageSequenceNo = mPageSequenceNo;
00070         retClone->mPageSize = mPageSize;
00071         retClone->mPageState = mPageState;
00072         retClone->mStreamSerialNo = mStreamSerialNo;
00073         retClone->mStructureVersion = mStructureVersion;
00074 
00075         //Copy the segment table.
00076         unsigned char* locBuff = new unsigned char[mNumPageSegments];                   //Stored in the returned classes member variable, deleted in it's destructor
00077         memcpy((void*)locBuff, (const void*)mSegmentTable, mNumPageSegments);
00078         retClone->mSegmentTable = locBuff;
00079         locBuff = NULL;         //Given away.
00080         //
00081 
00082         return retClone;
00083 }
00084 
00085 
00086 unsigned long OggPageHeader::calculateDataSize() {
00087         //Sums the bytes in the segment table to calculate the size of data.
00088         //FIX::: ??? No checks on pointers.
00089 
00090         unsigned long retDataSize = 0;
00091         for (int i = 0; i < mNumPageSegments; i++) {
00092                 retDataSize += mSegmentTable[i];
00093         }
00094 
00095         setDataSize(retDataSize);
00096         return retDataSize;
00097 }
00098 //Views pointer data only
00099 bool OggPageHeader::setBaseHeader(const unsigned char* inBaseHeader) {
00100         //This now does not delete the buffer
00101         
00102         //unsigned long locOffset = 0;  // unused
00103 
00104         //Check if the page has the correct capture pattern
00105         if (strncmp((const char*)inBaseHeader, "OggS", OGG_CAPTURE_PATTERN_SIZE) == 0) {
00106                 mStructureVersion = inBaseHeader[STRUCTURE_VERSION];
00107                 mHeaderFlags = inBaseHeader[HEADER_FLAGS];
00108                 mGranulePos = iLE_Math::CharArrToInt64(inBaseHeader + GRANULE_POS);
00109                 mStreamSerialNo = iLE_Math::charArrToULong(inBaseHeader + SERIAL_NO);
00110                 mPageSequenceNo = iLE_Math::charArrToULong(inBaseHeader + SEQUENCE_NO);
00111                 mCRCChecksum = iLE_Math::charArrToULong(inBaseHeader + OGG_CHECKSUM);
00112                 mNumPageSegments = inBaseHeader[NUM_SEGMENTS];
00113                 mHeaderSize = OGG_BASE_HEADER_SIZE + mNumPageSegments;
00114                 
00115                 mPageSize = mHeaderSize + mDataSize;
00116                 mPageState = BASE_HEAD_SET;
00117                 return true;
00118         } else {
00119                 return false;
00120         }
00121 
00123                 //setStructureVersion(inBaseHeader[locOffset]);
00124                 //locOffset++;
00125 
00127                 //setHeaderFlags(inBaseHeader[locOffset]);
00128                 //locOffset++;
00129 
00131                 //setGranulePos((const unsigned char*)(inBaseHeader + locOffset));
00132                 //locOffset += 8;
00133 
00135                 //setStreamSerialNo((const unsigned char*)(inBaseHeader + locOffset));
00136                 //locOffset += 4;
00137 
00139                 //setPageSequenceNo(inBaseHeader + locOffset);
00140                 //locOffset += 4;
00141 
00143                 //setCRCChecksum(inBaseHeader + locOffset);
00144                 //locOffset += 4;
00145 
00147                 //setNumPageSegments(inBaseHeader[locOffset]);
00148                 //locOffset++;
00149 
00151                 //setHeaderSize(OGG_BASE_HEADER_SIZE + mNumPageSegments);
00152 
00153                 //mPageState = BASE_HEAD_SET;
00154 
00155                                 
00156         
00157 
00158 }
00159 
00160 
00161 void OggPageHeader::setPageState(ePageState inPageState) 
00162 {
00163         mPageState = inPageState;
00164 }
00165 unsigned char OggPageHeader::StructureVersion() 
00166 {
00167         return mStructureVersion;
00168 }
00169 
00170 unsigned char OggPageHeader::HeaderFlags() 
00171 {
00172         return mHeaderFlags;
00173 }
00174 
00175 LOOG_INT64 OggPageHeader::GranulePos()
00176 {
00177         return mGranulePos;
00178 }
00179 
00180 unsigned long OggPageHeader::StreamSerialNo()
00181 {
00182         return mStreamSerialNo;
00183 }
00184 unsigned long OggPageHeader::PageSequenceNo()
00185 {
00186         return mPageSequenceNo;
00187 }
00188 unsigned long OggPageHeader::CRCChecksum()
00189 {
00190         return mCRCChecksum;
00191 }
00192 
00193 unsigned char OggPageHeader::NumPageSegments()
00194 {
00195         return mNumPageSegments;
00196 }
00197 unsigned char* OggPageHeader::SegmentTable()
00198 {
00199         return mSegmentTable;
00200 }
00201 
00202 
00203 
00204 void OggPageHeader::setStructureVersion(unsigned char inVal) 
00205 {
00206         mStructureVersion = inVal;
00207 }
00208 
00209 void OggPageHeader::setHeaderFlags(unsigned char inVal) 
00210 {
00211         mHeaderFlags = inVal;
00212 }
00213 
00214 void OggPageHeader::setGranulePos(LOOG_INT64 inGranulePos)
00215 {
00216         mGranulePos = inGranulePos;
00217 }
00218 void OggPageHeader::setGranulePos(const unsigned char* inPtr)
00219 {
00220         mGranulePos = iLE_Math::CharArrToInt64(inPtr);
00221 }
00222 
00223 void OggPageHeader::setStreamSerialNo(unsigned long inVal)
00224 {
00225         mStreamSerialNo = inVal;
00226 }
00227 void OggPageHeader::setStreamSerialNo(const unsigned char* inPtr)
00228 {
00229         mStreamSerialNo = iLE_Math::charArrToULong(inPtr);
00230 }
00231 void OggPageHeader::setPageSequenceNo(unsigned long inVal)
00232 {
00233         mPageSequenceNo = inVal;
00234 }
00235 void OggPageHeader::setPageSequenceNo(const unsigned char* inPtr)
00236 {
00237         mPageSequenceNo = iLE_Math::charArrToULong(inPtr);;
00238 }
00239 
00240 void OggPageHeader::setCRCChecksum(unsigned long inVal)
00241 {
00242         mCRCChecksum = inVal;
00243 }
00244 void OggPageHeader::setCRCChecksum(const unsigned char* inPtr)
00245 {
00246         mCRCChecksum = iLE_Math::charArrToULong(inPtr);;
00247 }
00248 
00249 void OggPageHeader::setNumPageSegments(unsigned char inVal)
00250 {
00251         if (mSegmentTable == NULL) {
00252                 mNumPageSegments = inVal;
00253         }
00254 }
00256 //      Copies the data from the pointer it is given.
00257 //      
00258 void OggPageHeader::setSegmentTable(const unsigned char* inPtr, unsigned char inNumSegs) 
00259 {
00260         unsigned char* locSegTable = new unsigned char[inNumSegs];              //This is stored in the member variable and deleted in the destructor.
00261         memcpy((void*)locSegTable, (const void*)inPtr, inNumSegs);
00262         delete[] mSegmentTable;
00263         mSegmentTable = locSegTable;
00264         mNumPageSegments = inNumSegs;
00265         setHeaderSize(OggPageHeader::OGG_BASE_HEADER_SIZE + inNumSegs);
00266 }
00267 void OggPageHeader::setSegmentTable(unsigned char* inPtr) {
00268         delete[] mSegmentTable;
00269         mSegmentTable = inPtr;
00270 
00271 }
00272 
00273 OggPageHeader::ePageState OggPageHeader::pageState() {
00274         return mPageState;
00275 }
00276 unsigned long OggPageHeader::pageSize()
00277 {
00278         return mPageSize;
00279 }
00280 unsigned long OggPageHeader::headerSize()
00281 {
00282         return mHeaderSize;
00283 }
00284 unsigned long OggPageHeader::dataSize()
00285 {
00286         return mDataSize;
00287 }
00288 bool OggPageHeader::rawData(unsigned char* outData, unsigned long inBuffSize) {
00289         
00290         //0-3                   CapPattern                                              "OggS"
00291         //4                             Struct Ver
00292         //5                             Head Flags
00293         //6-13                  Granule Pos
00294         //14-17                 Stream Serial No
00295         //18-21                 Page Seq No
00296         //22-25                 CheckSum
00297         //26                    Num Segments
00298         //27...                 SegmentTable
00299         //
00300 
00301         if (mHeaderSize > inBuffSize) {
00302                 return false;
00303         }
00304         outData[0] = 'O';
00305         outData[1] = 'g';
00306         outData[2] = 'g';
00307         outData[3] = 'S';
00308         outData[4] = mStructureVersion;
00309         outData[5] = mHeaderFlags;
00310         iLE_Math::Int64ToCharArr(mGranulePos, &outData[6]);
00311         iLE_Math::ULongToCharArr(mStreamSerialNo, &outData[14]);
00312         iLE_Math::ULongToCharArr(mPageSequenceNo, &outData[18]);
00313         iLE_Math::ULongToCharArr(mCRCChecksum, &outData[22]);
00314         outData[26] = mNumPageSegments;
00315 
00316         //TODO::: Validate the length of all this.
00317         memcpy((void*)(outData + 27), (const void*)mSegmentTable, mNumPageSegments);
00318 
00319         return true;
00320 }
00321 
00322 //unsigned long OggPageHeader::headerCheckSum() 
00323 //{
00324 //      
00325 //
00326 //}
00327 
00328 
00329 bool OggPageHeader::isBOS() {
00330         return ((mHeaderFlags & BOS) != 0 );
00331 }
00332 bool OggPageHeader::isEOS() {
00333         return ((mHeaderFlags & EOS) != 0 );
00334 }
00335 
00336 bool OggPageHeader::isContinuation() {
00337         return ((mHeaderFlags & CONTINUATION) != 0 );
00338 }
00339 
00340 
00341 string OggPageHeader::toString() {
00342 
00343         string retStr = "Ver No      : " + StringHelper::numToString((unsigned int)mStructureVersion) + "\n";
00344         
00345         retStr +=               "Head Flags  :";
00346         if ((mHeaderFlags & CONTINUATION) != 0) {
00347                 retStr += " continuation";
00348         }
00349         if ((mHeaderFlags & BOS) != 0) {
00350                 retStr += " bos";
00351         }
00352         if ((mHeaderFlags & EOS) != 0) {
00353                 retStr += " eos";
00354         }
00355         retStr += "\n";
00356 
00357         retStr +=               "Granule Pos : " + StringHelper::numToString(mGranulePos) + "\n";
00358         retStr +=               "Serial No   : " + StringHelper::numToString(mStreamSerialNo) + "\n";
00359         retStr +=               "Seq No      : " + StringHelper::numToString(mPageSequenceNo) + "\n";
00360         retStr +=               "Checksum    : " + StringHelper::numToString(mCRCChecksum) + "\n";
00361         retStr +=               "Num Segs    : " + StringHelper::numToString((unsigned int)mNumPageSegments) + "\n";
00362         retStr +=               "------------------------\n";
00363         retStr +=               "Head Size   : " + StringHelper::numToString(mHeaderSize) + "\n";
00364         retStr +=               "Data Size   : " + StringHelper::numToString(mDataSize) + "\n";
00365         retStr +=               "Page Size   : " + StringHelper::numToString(mPageSize) +"\n";
00366         
00367         return retStr;
00368 }
00369 
00370 
00371 //This could be unsigned short.
00372 void OggPageHeader::setHeaderSize(unsigned long inVal)
00373 {
00374         mHeaderSize = inVal;
00375         mPageSize = mHeaderSize + mDataSize;
00376 }
00377 void OggPageHeader::setDataSize(unsigned long inVal)
00378 {
00379         mDataSize = inVal;
00380         mPageSize = mHeaderSize + mDataSize;
00381 }
00382 

Generated on Tue Feb 15 14:54:21 2005 for oggdsf by  doxygen 1.3.9