StreamParser Class Reference

#include <StreamParser.hh>

Inheritance diagram for StreamParser:

Inheritance graph
[legend]
Collaboration diagram for StreamParser:

Collaboration graph
[legend]

Public Member Functions

virtual void flushInput ()

Protected Types

typedef void( clientContinueFunc )(void *clientData, unsigned char *ptr, unsigned size, struct timeval presentationTime)

Protected Member Functions

 StreamParser (FramedSource *inputSource, FramedSource::onCloseFunc *onInputCloseFunc, void *onInputCloseClientData, clientContinueFunc *clientContinueFunc, void *clientContinueClientData)
virtual ~StreamParser ()
void saveParserState ()
virtual void restoreSavedParserState ()
u_int32_t get4Bytes ()
u_int32_t test4Bytes ()
u_int16_t get2Bytes ()
u_int8_t get1Byte ()
void getBytes (u_int8_t *to, unsigned numBytes)
void skipBytes (unsigned numBytes)
void skipBits (unsigned numBits)
unsigned getBits (unsigned numBits)
unsigned curOffset () const
unsigned & totNumValidBytes ()

Private Member Functions

unsigned char * curBank ()
unsigned char * nextToParse ()
unsigned char * lastParsed ()
void ensureValidBytes (unsigned numBytesNeeded)
void ensureValidBytes1 (unsigned numBytesNeeded)

Static Private Member Functions

static void afterGettingBytes (void *clientData, unsigned numBytesRead, unsigned numTruncatedBytes, struct timeval presentationTime, unsigned durationInMicroseconds)

Private Attributes

FramedSourcefInputSource
FramedSource::onCloseFuncfOnInputCloseFunc
void * fOnInputCloseClientData
clientContinueFuncfClientContinueFunc
void * fClientContinueClientData
unsigned char * fBank [2]
unsigned char fCurBankNum
unsigned char * fCurBank
unsigned fSavedParserIndex
unsigned char fSavedRemainingUnparsedBits
unsigned fCurParserIndex
unsigned char fRemainingUnparsedBits
unsigned fTotNumValidBytes

Detailed Description

Definition at line 28 of file StreamParser.hh.


Member Typedef Documentation

typedef void( StreamParser::clientContinueFunc)(void *clientData, unsigned char *ptr, unsigned size, struct timeval presentationTime) [protected]

Definition at line 33 of file StreamParser.hh.


Constructor & Destructor Documentation

StreamParser::StreamParser ( FramedSource inputSource,
FramedSource::onCloseFunc onInputCloseFunc,
void *  onInputCloseClientData,
clientContinueFunc clientContinueFunc,
void *  clientContinueClientData 
) [protected]

Definition at line 28 of file StreamParser.cpp.

References BANK_SIZE, fBank, fCurBank, and fCurBankNum.

00033   : fInputSource(inputSource), fOnInputCloseFunc(onInputCloseFunc),
00034     fOnInputCloseClientData(onInputCloseClientData),
00035     fClientContinueFunc(clientContinueFunc),
00036     fClientContinueClientData(clientContinueClientData),
00037     fSavedParserIndex(0), fSavedRemainingUnparsedBits(0),
00038     fCurParserIndex(0), fRemainingUnparsedBits(0),
00039     fTotNumValidBytes(0) {
00040   fBank[0] = new unsigned char[BANK_SIZE];
00041   fBank[1] = new unsigned char[BANK_SIZE];
00042   fCurBankNum = 0;
00043   fCurBank = fBank[fCurBankNum];
00044 }

StreamParser::~StreamParser (  )  [protected, virtual]

Definition at line 46 of file StreamParser.cpp.

References fBank.

00046                             {
00047   delete[] fBank[0]; delete[] fBank[1];
00048 }


Member Function Documentation

void StreamParser::flushInput (  )  [virtual]

Reimplemented in MPEG1or2VideoStreamParser, and MPEG4VideoStreamParser.

Definition at line 178 of file StreamParser.cpp.

References fCurParserIndex, fRemainingUnparsedBits, fSavedParserIndex, fSavedRemainingUnparsedBits, and fTotNumValidBytes.

Referenced by MPEGVideoStreamFramer::flushInput(), MPEG4VideoStreamParser::flushInput(), MPEG1or2VideoStreamParser::flushInput(), MPEG1or2Demux::flushInput(), MPEG1or2AudioStreamFramer::flushInput(), and AC3AudioStreamFramer::flushInput().

void StreamParser::saveParserState (  )  [protected]

Definition at line 121 of file StreamParser.cpp.

References fCurParserIndex, fRemainingUnparsedBits, fSavedParserIndex, and fSavedRemainingUnparsedBits.

Referenced by MPEG1or2AudioStreamParser::parse(), AC3AudioStreamParser::parseFrame(), MPEGVideoStreamParser::setParseState(), MPEGProgramStreamParser::setParseState(), and H263plusVideoStreamParser::setParseState().

void StreamParser::restoreSavedParserState (  )  [protected, virtual]

Reimplemented in H263plusVideoStreamParser, and MPEGVideoStreamParser.

Definition at line 126 of file StreamParser.cpp.

References fCurParserIndex, fRemainingUnparsedBits, fSavedParserIndex, and fSavedRemainingUnparsedBits.

Referenced by afterGettingBytes(), MPEGProgramStreamParser::parsePESPacket(), MPEGVideoStreamParser::restoreSavedParserState(), and H263plusVideoStreamParser::restoreSavedParserState().

u_int32_t StreamParser::get4Bytes (  )  [inline, protected]

Definition at line 46 of file StreamParser.hh.

References fCurParserIndex, fRemainingUnparsedBits, and test4Bytes().

Referenced by AC3AudioStreamParser::parseFrame(), MPEG1or2VideoStreamParser::parseGOPHeader(), MPEG4VideoStreamParser::parseGroupOfVideoObjectPlane(), MPEGProgramStreamParser::parsePackHeader(), MPEGProgramStreamParser::parsePESPacket(), MPEG1or2VideoStreamParser::parsePictureHeader(), MPEG4VideoStreamParser::parseVideoObjectLayer(), MPEG4VideoStreamParser::parseVideoObjectPlane(), MPEG1or2VideoStreamParser::parseVideoSequenceHeader(), MPEG4VideoStreamParser::parseVisualObject(), MPEG4VideoStreamParser::parseVisualObjectSequence(), MPEGVideoStreamParser::saveToNextCode(), and MPEGVideoStreamParser::skipToNextCode().

00046                         { // byte-aligned; returned in big-endian order
00047     u_int32_t result = test4Bytes();
00048     fCurParserIndex += 4;
00049     fRemainingUnparsedBits = 0;
00050 
00051     return result;
00052   }

u_int32_t StreamParser::test4Bytes (  )  [inline, protected]

Definition at line 53 of file StreamParser.hh.

References ensureValidBytes(), and nextToParse().

Referenced by get4Bytes(), getBits(), MPEG1or2AudioStreamParser::parse(), AC3AudioStreamParser::parseFrame(), MPEG1or2VideoStreamParser::parseGOPHeader(), MPEGProgramStreamParser::parsePackHeader(), MPEGProgramStreamParser::parsePESPacket(), MPEGProgramStreamParser::parseSystemHeader(), MPEG1or2VideoStreamParser::parseVideoSequenceHeader(), and MPEG4VideoStreamParser::parseVisualObjectSequence().

00053                          { // as above, but doesn't advance ptr
00054     ensureValidBytes(4);
00055 
00056     unsigned char const* ptr = nextToParse();
00057     return (ptr[0]<<24)|(ptr[1]<<16)|(ptr[2]<<8)|ptr[3];
00058   }

u_int16_t StreamParser::get2Bytes (  )  [inline, protected]

Definition at line 60 of file StreamParser.hh.

References ensureValidBytes(), fCurParserIndex, fRemainingUnparsedBits, and nextToParse().

Referenced by MPEGProgramStreamParser::parsePESPacket(), and MPEGProgramStreamParser::parseSystemHeader().

00060                         {
00061     ensureValidBytes(2);
00062 
00063     unsigned char const* ptr = nextToParse();
00064     u_int16_t result = (ptr[0]<<8)|ptr[1];
00065 
00066     fCurParserIndex += 2;
00067     fRemainingUnparsedBits = 0;
00068 
00069     return result;
00070   }

u_int8_t StreamParser::get1Byte (  )  [inline, protected]

Definition at line 72 of file StreamParser.hh.

References curBank(), ensureValidBytes(), fCurParserIndex, and fRemainingUnparsedBits.

Referenced by MPEG1or2VideoStreamParser::parseGOPHeader(), H263plusVideoStreamParser::parseH263Frame(), MPEGProgramStreamParser::parsePackHeader(), MPEGProgramStreamParser::parsePESPacket(), MPEG4VideoStreamParser::parseVideoObjectPlane(), MPEG1or2VideoStreamParser::parseVideoSequenceHeader(), MPEG4VideoStreamParser::parseVisualObject(), MPEG4VideoStreamParser::parseVisualObjectSequence(), MPEGVideoStreamParser::saveToNextCode(), and MPEGVideoStreamParser::skipToNextCode().

00072                       { // byte-aligned
00073     ensureValidBytes(1);
00074     fRemainingUnparsedBits = 0;
00075     return curBank()[fCurParserIndex++];
00076   }

void StreamParser::getBytes ( u_int8_t *  to,
unsigned  numBytes 
) [inline, protected]

Definition at line 78 of file StreamParser.hh.

References ensureValidBytes(), fCurParserIndex, fRemainingUnparsedBits, and nextToParse().

Referenced by MPEG1or2AudioStreamParser::parse(), AC3AudioStreamParser::parseFrame(), MPEG4VideoStreamParser::parseGroupOfVideoObjectPlane(), H263plusVideoStreamParser::parseH263Frame(), and MPEGProgramStreamParser::parsePESPacket().

00078                                                  {
00079     ensureValidBytes(numBytes);
00080     memmove(to, nextToParse(), numBytes);
00081     fCurParserIndex += numBytes;
00082     fRemainingUnparsedBits = 0;
00083   }

void StreamParser::skipBytes ( unsigned  numBytes  )  [inline, protected]

Definition at line 84 of file StreamParser.hh.

References ensureValidBytes(), and fCurParserIndex.

Referenced by MPEG1or2AudioStreamParser::parse(), AC3AudioStreamParser::parseFrame(), MPEGProgramStreamParser::parsePackHeader(), MPEGProgramStreamParser::parsePESPacket(), and MPEGProgramStreamParser::parseSystemHeader().

00084                                     {
00085     ensureValidBytes(numBytes);
00086     fCurParserIndex += numBytes;
00087   }

void StreamParser::skipBits ( unsigned  numBits  )  [protected]

Definition at line 131 of file StreamParser.cpp.

References ensureValidBytes(), fCurParserIndex, and fRemainingUnparsedBits.

Referenced by MPEGProgramStreamParser::parsePackHeader().

00131                                             {
00132   if (numBits <= fRemainingUnparsedBits) {
00133     fRemainingUnparsedBits -= numBits;
00134   } else {
00135     numBits -= fRemainingUnparsedBits;
00136 
00137     unsigned numBytesToExamine = (numBits+7)/8; // round up
00138     ensureValidBytes(numBytesToExamine);
00139     fCurParserIndex += numBytesToExamine;
00140 
00141     fRemainingUnparsedBits = 8*numBytesToExamine - numBits;
00142   }
00143 }

unsigned StreamParser::getBits ( unsigned  numBits  )  [protected]

Definition at line 145 of file StreamParser.cpp.

References fCurParserIndex, fRemainingUnparsedBits, lastParsed(), and test4Bytes().

Referenced by MPEGProgramStreamParser::parsePackHeader(), and MPEGProgramStreamParser::parsePESPacket().

00145                                                {
00146   if (numBits <= fRemainingUnparsedBits) {
00147     unsigned char lastByte = *lastParsed();
00148     lastByte >>= (fRemainingUnparsedBits - numBits);
00149     fRemainingUnparsedBits -= numBits;
00150 
00151     return (unsigned)lastByte &~ ((~0)<<numBits);
00152   } else {
00153     unsigned char lastByte;
00154     if (fRemainingUnparsedBits > 0) {
00155       lastByte = *lastParsed();
00156     } else {
00157       lastByte = 0;
00158     }
00159 
00160     unsigned remainingBits = numBits - fRemainingUnparsedBits; // > 0
00161 
00162     // For simplicity, read the next 4 bytes, even though we might not
00163     // need all of them here:
00164     unsigned result = test4Bytes();
00165 
00166     result >>= (32 - remainingBits);
00167     result |= (lastByte << remainingBits);
00168     if (numBits < 32) result &=~ ((~0)<<numBits);
00169 
00170     unsigned const numRemainingBytes = (remainingBits+7)/8;
00171     fCurParserIndex += numRemainingBytes;
00172     fRemainingUnparsedBits = 8*numRemainingBytes - remainingBits;
00173 
00174     return result;
00175   }
00176 }

unsigned StreamParser::curOffset (  )  const [inline, protected]

Definition at line 93 of file StreamParser.hh.

References fCurParserIndex.

Referenced by MPEGProgramStreamParser::parsePESPacket().

00093 { return fCurParserIndex; }

unsigned& StreamParser::totNumValidBytes (  )  [inline, protected]

Definition at line 95 of file StreamParser.hh.

References fTotNumValidBytes.

Referenced by AC3AudioStreamParser::testStreamCode().

00095 { return fTotNumValidBytes; }

unsigned char* StreamParser::curBank (  )  [inline, private]

Definition at line 98 of file StreamParser.hh.

References fCurBank.

Referenced by afterGettingBytes(), ensureValidBytes1(), get1Byte(), lastParsed(), and nextToParse().

00098 { return fCurBank; }

unsigned char* StreamParser::nextToParse (  )  [inline, private]

Definition at line 99 of file StreamParser.hh.

References curBank(), and fCurParserIndex.

Referenced by get2Bytes(), getBytes(), and test4Bytes().

00099 { return &curBank()[fCurParserIndex]; }

unsigned char* StreamParser::lastParsed (  )  [inline, private]

Definition at line 100 of file StreamParser.hh.

References curBank(), and fCurParserIndex.

Referenced by getBits().

00100 { return &curBank()[fCurParserIndex-1]; }

void StreamParser::ensureValidBytes ( unsigned  numBytesNeeded  )  [inline, private]

Definition at line 103 of file StreamParser.hh.

References ensureValidBytes1(), fCurParserIndex, and fTotNumValidBytes.

Referenced by get1Byte(), get2Bytes(), getBytes(), skipBits(), skipBytes(), and test4Bytes().

00103                                                  {
00104     // common case: inlined:
00105     if (fCurParserIndex + numBytesNeeded <= fTotNumValidBytes) return;
00106 
00107     ensureValidBytes1(numBytesNeeded);
00108   }

void StreamParser::ensureValidBytes1 ( unsigned  numBytesNeeded  )  [private]

Definition at line 52 of file StreamParser.cpp.

References afterGettingBytes(), BANK_SIZE, curBank(), Medium::envir(), fBank, fCurBank, fCurBankNum, fCurParserIndex, fInputSource, fOnInputCloseClientData, fOnInputCloseFunc, fSavedParserIndex, fTotNumValidBytes, FramedSource::getNextFrame(), FramedSource::maxFrameSize(), and NO_MORE_BUFFERED_INPUT.

Referenced by ensureValidBytes().

00052                                                             {
00053   // We need to read some more bytes from the input source.
00054   // First, clarify how much data to ask for:
00055   unsigned maxInputFrameSize = fInputSource->maxFrameSize();
00056   if (maxInputFrameSize > numBytesNeeded) numBytesNeeded = maxInputFrameSize;
00057 
00058   // First, check whether these new bytes would overflow the current
00059   // bank.  If so, start using a new bank now.
00060   if (fCurParserIndex + numBytesNeeded > BANK_SIZE) {
00061     // Swap banks, but save any still-needed bytes from the old bank:
00062     unsigned numBytesToSave = fTotNumValidBytes - fSavedParserIndex;
00063     unsigned char const* from = &curBank()[fSavedParserIndex];
00064 
00065     fCurBankNum = (fCurBankNum + 1)%2;
00066     fCurBank = fBank[fCurBankNum];
00067     memmove(curBank(), from, numBytesToSave);
00068     fCurParserIndex = fCurParserIndex - fSavedParserIndex;
00069     fSavedParserIndex = 0;
00070     fTotNumValidBytes = numBytesToSave;
00071   }
00072 
00073   // ASSERT: fCurParserIndex + numBytesNeeded > fTotNumValidBytes
00074   //      && fCurParserIndex + numBytesNeeded <= BANK_SIZE
00075   if (fCurParserIndex + numBytesNeeded > BANK_SIZE) {
00076     // If this happens, it means that we have too much saved parser state.
00077     // To fix this, increase BANK_SIZE as appropriate.
00078     fInputSource->envir() << "StreamParser internal error ("
00079                           << fCurParserIndex << "+ "
00080                           << numBytesNeeded << " > "
00081                           << BANK_SIZE << ")\n";
00082     exit(1);
00083   }
00084 
00085   // Try to read as many new bytes as will fit in the current bank:
00086   unsigned maxNumBytesToRead = BANK_SIZE - fTotNumValidBytes;
00087   fInputSource->getNextFrame(&curBank()[fTotNumValidBytes],
00088                              maxNumBytesToRead,
00089                              afterGettingBytes, this,
00090                              fOnInputCloseFunc, fOnInputCloseClientData);
00091 
00092   throw NO_MORE_BUFFERED_INPUT;
00093 }

void StreamParser::afterGettingBytes ( void *  clientData,
unsigned  numBytesRead,
unsigned  numTruncatedBytes,
struct timeval  presentationTime,
unsigned  durationInMicroseconds 
) [static, private]

Definition at line 95 of file StreamParser.cpp.

References BANK_SIZE, curBank(), Medium::envir(), fClientContinueClientData, fClientContinueFunc, fInputSource, fTotNumValidBytes, and restoreSavedParserState().

Referenced by ensureValidBytes1().

00099                                                                          {
00100   StreamParser* buffer = (StreamParser*)clientData;
00101 
00102   // Sanity check: Make sure we didn't get too many bytes for our bank:
00103   if (buffer->fTotNumValidBytes + numBytesRead > BANK_SIZE) {
00104     buffer->fInputSource->envir()
00105       << "StreamParser::afterGettingBytes() warning: read "
00106       << numBytesRead << " bytes; expected no more than "
00107       << BANK_SIZE - buffer->fTotNumValidBytes << "\n";
00108   }
00109 
00110   unsigned char* ptr = &buffer->curBank()[buffer->fTotNumValidBytes];
00111   buffer->fTotNumValidBytes += numBytesRead;
00112 
00113   // Continue our original calling source where it left off:
00114   buffer->restoreSavedParserState();
00115       // Sigh... this is a crock; things would have been a lot simpler
00116       // here if we were using threads, with synchronous I/O...
00117   buffer->fClientContinueFunc(buffer->fClientContinueClientData,
00118                               ptr, numBytesRead, presentationTime);
00119 }


Field Documentation

FramedSource* StreamParser::fInputSource [private]

Definition at line 117 of file StreamParser.hh.

Referenced by afterGettingBytes(), and ensureValidBytes1().

FramedSource::onCloseFunc* StreamParser::fOnInputCloseFunc [private]

Definition at line 118 of file StreamParser.hh.

Referenced by ensureValidBytes1().

void* StreamParser::fOnInputCloseClientData [private]

Definition at line 119 of file StreamParser.hh.

Referenced by ensureValidBytes1().

clientContinueFunc* StreamParser::fClientContinueFunc [private]

Definition at line 120 of file StreamParser.hh.

Referenced by afterGettingBytes().

void* StreamParser::fClientContinueClientData [private]

Definition at line 121 of file StreamParser.hh.

Referenced by afterGettingBytes().

unsigned char* StreamParser::fBank[2] [private]

Definition at line 124 of file StreamParser.hh.

Referenced by ensureValidBytes1(), StreamParser(), and ~StreamParser().

unsigned char StreamParser::fCurBankNum [private]

Definition at line 125 of file StreamParser.hh.

Referenced by ensureValidBytes1(), and StreamParser().

unsigned char* StreamParser::fCurBank [private]

Definition at line 126 of file StreamParser.hh.

Referenced by curBank(), ensureValidBytes1(), and StreamParser().

unsigned StreamParser::fSavedParserIndex [private]

Definition at line 129 of file StreamParser.hh.

Referenced by ensureValidBytes1(), flushInput(), restoreSavedParserState(), and saveParserState().

unsigned char StreamParser::fSavedRemainingUnparsedBits [private]

Definition at line 130 of file StreamParser.hh.

Referenced by flushInput(), restoreSavedParserState(), and saveParserState().

unsigned StreamParser::fCurParserIndex [private]

Definition at line 133 of file StreamParser.hh.

Referenced by curOffset(), ensureValidBytes(), ensureValidBytes1(), flushInput(), get1Byte(), get2Bytes(), get4Bytes(), getBits(), getBytes(), lastParsed(), nextToParse(), restoreSavedParserState(), saveParserState(), skipBits(), and skipBytes().

unsigned char StreamParser::fRemainingUnparsedBits [private]

Definition at line 134 of file StreamParser.hh.

Referenced by flushInput(), get1Byte(), get2Bytes(), get4Bytes(), getBits(), getBytes(), restoreSavedParserState(), saveParserState(), and skipBits().

unsigned StreamParser::fTotNumValidBytes [private]

Definition at line 137 of file StreamParser.hh.

Referenced by afterGettingBytes(), ensureValidBytes(), ensureValidBytes1(), flushInput(), and totNumValidBytes().


The documentation for this class was generated from the following files:
Generated on Tue Oct 7 15:40:11 2008 for live by  doxygen 1.5.2