liveMedia/MPEGVideoStreamParser.hh

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // "liveMedia"
00017 // Copyright (c) 1996-2013 Live Networks, Inc.  All rights reserved.
00018 // An abstract parser for MPEG video streams
00019 // C++ header
00020 
00021 #ifndef _MPEG_VIDEO_STREAM_PARSER_HH
00022 #define _MPEG_VIDEO_STREAM_PARSER_HH
00023 
00024 #ifndef _STREAM_PARSER_HH
00025 #include "StreamParser.hh"
00026 #endif
00027 #ifndef _MPEG_VIDEO_STREAM_FRAMER_HH
00028 #include "MPEGVideoStreamFramer.hh"
00029 #endif
00030 
00032 
00033 class MPEGVideoStreamParser: public StreamParser {
00034 public:
00035   MPEGVideoStreamParser(MPEGVideoStreamFramer* usingSource,
00036                         FramedSource* inputSource);
00037   virtual ~MPEGVideoStreamParser();
00038 
00039 public:
00040   void registerReadInterest(unsigned char* to, unsigned maxSize);
00041 
00042   virtual unsigned parse() = 0;
00043       // returns the size of the frame that was acquired, or 0 if none was
00044       // The number of truncated bytes (if any) is given by:
00045   unsigned numTruncatedBytes() const { return fNumTruncatedBytes; }
00046 
00047 protected:
00048   void setParseState() {
00049     fSavedTo = fTo;
00050     fSavedNumTruncatedBytes = fNumTruncatedBytes;
00051     saveParserState();
00052   }
00053 
00054   // Record "byte" in the current output frame:
00055   void saveByte(u_int8_t byte) {
00056     if (fTo >= fLimit) { // there's no space left
00057       ++fNumTruncatedBytes;
00058       return;
00059     }
00060 
00061     *fTo++ = byte;
00062   }
00063 
00064   void save4Bytes(u_int32_t word) {
00065     if (fTo+4 > fLimit) { // there's no space left
00066       fNumTruncatedBytes += 4;
00067       return;
00068     }
00069 
00070     *fTo++ = word>>24; *fTo++ = word>>16; *fTo++ = word>>8; *fTo++ = word;
00071   }
00072 
00073   // Save data until we see a sync word (0x000001xx):
00074   void saveToNextCode(u_int32_t& curWord) {
00075     saveByte(curWord>>24);
00076     curWord = (curWord<<8)|get1Byte();
00077     while ((curWord&0xFFFFFF00) != 0x00000100) {
00078       if ((unsigned)(curWord&0xFF) > 1) {
00079         // a sync word definitely doesn't begin anywhere in "curWord"
00080         save4Bytes(curWord);
00081         curWord = get4Bytes();
00082       } else {
00083         // a sync word might begin in "curWord", although not at its start
00084         saveByte(curWord>>24);
00085         unsigned char newByte = get1Byte();
00086         curWord = (curWord<<8)|newByte;
00087       }
00088     }
00089   }
00090 
00091   // Skip data until we see a sync word (0x000001xx):
00092   void skipToNextCode(u_int32_t& curWord) {
00093     curWord = (curWord<<8)|get1Byte();
00094     while ((curWord&0xFFFFFF00) != 0x00000100) {
00095       if ((unsigned)(curWord&0xFF) > 1) {
00096         // a sync word definitely doesn't begin anywhere in "curWord"
00097         curWord = get4Bytes();
00098       } else {
00099         // a sync word might begin in "curWord", although not at its start
00100         unsigned char newByte = get1Byte();
00101         curWord = (curWord<<8)|newByte;
00102       }
00103     }
00104   }
00105 
00106 protected:
00107   MPEGVideoStreamFramer* fUsingSource;
00108 
00109   // state of the frame that's currently being read:
00110   unsigned char* fStartOfFrame;
00111   unsigned char* fTo;
00112   unsigned char* fLimit;
00113   unsigned fNumTruncatedBytes;
00114   unsigned curFrameSize() { return fTo - fStartOfFrame; }
00115   unsigned char* fSavedTo;
00116   unsigned fSavedNumTruncatedBytes;
00117 
00118 private: // redefined virtual functions
00119   virtual void restoreSavedParserState();
00120 };
00121 
00122 #endif

Generated on Mon Apr 29 13:28:03 2013 for live by  doxygen 1.5.2