liveMedia/MatroskaFileParser.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-2014 Live Networks, Inc.  All rights reserved.
00018 // A parser for a Matroska file.
00019 // C++ header
00020 
00021 #ifndef _MATROSKA_FILE_PARSER_HH
00022 
00023 #ifndef _STREAM_PARSER_HH
00024 #include "StreamParser.hh"
00025 #endif
00026 #ifndef _MATROSKA_FILE_HH
00027 #include "MatroskaFile.hh"
00028 #endif
00029 #ifndef _EBML_NUMBER_HH
00030 #include "EBMLNumber.hh"
00031 #endif
00032 
00033 // An enum representing the current state of the parser:
00034 enum MatroskaParseState {
00035   PARSING_START_OF_FILE,
00036   LOOKING_FOR_TRACKS,
00037   PARSING_TRACK,
00038   PARSING_CUES,
00039   LOOKING_FOR_CLUSTER,
00040   LOOKING_FOR_BLOCK,
00041   PARSING_BLOCK,
00042   DELIVERING_FRAME_WITHIN_BLOCK,
00043   DELIVERING_FRAME_BYTES
00044 };
00045 
00046 class MatroskaFileParser: public StreamParser {
00047 public:
00048   MatroskaFileParser(MatroskaFile& ourFile, FramedSource* inputSource,
00049                      FramedSource::onCloseFunc* onEndFunc, void* onEndClientData,
00050                      MatroskaDemux* ourDemux = NULL);
00051   virtual ~MatroskaFileParser();
00052 
00053   void seekToTime(double& seekNPT);
00054 
00055   // StreamParser 'client continue' function:
00056   static void continueParsing(void* clientData, unsigned char* ptr, unsigned size, struct timeval presentationTime);
00057   void continueParsing();
00058 
00059 private:
00060   // Parsing functions:
00061   Boolean parse();
00062     // returns True iff we have finished parsing to the end of all 'Track' headers (on initialization)
00063 
00064   Boolean parseStartOfFile();
00065   void lookForNextTrack();
00066   Boolean parseTrack();
00067   Boolean parseCues();
00068 
00069   void lookForNextBlock();
00070   void parseBlock();
00071   Boolean deliverFrameWithinBlock();
00072   void deliverFrameBytes();
00073 
00074   void getCommonFrameBytes(MatroskaTrack* track, u_int8_t* to, unsigned numBytesToGet, unsigned numBytesToSkip);
00075 
00076   Boolean parseEBMLNumber(EBMLNumber& num);
00077   Boolean parseEBMLIdAndSize(EBMLId& id, EBMLDataSize& size);
00078   Boolean parseEBMLVal_unsigned64(EBMLDataSize& size, u_int64_t& result);
00079   Boolean parseEBMLVal_unsigned(EBMLDataSize& size, unsigned& result);
00080   Boolean parseEBMLVal_float(EBMLDataSize& size, float& result);
00081   Boolean parseEBMLVal_string(EBMLDataSize& size, char*& result);
00082     // Note: "result" is dynamically allocated; the caller must delete[] it later
00083   Boolean parseEBMLVal_binary(EBMLDataSize& size, u_int8_t*& result);
00084     // Note: "result" is dynamically allocated; the caller must delete[] it later
00085   void skipHeader(EBMLDataSize const& size);
00086   void skipRemainingHeaderBytes(Boolean isContinuation);
00087 
00088   void setParseState();
00089 
00090   void seekToFilePosition(u_int64_t offsetInFile);
00091   void seekToEndOfFile();
00092   void resetStateAfterSeeking(); // common code, called by both of the above
00093 
00094 private: // redefined virtual functions
00095   virtual void restoreSavedParserState();
00096 
00097 private:
00098   // General state for parsing:
00099   MatroskaFile& fOurFile;
00100   FramedSource* fInputSource;
00101   FramedSource::onCloseFunc* fOnEndFunc;
00102   void* fOnEndClientData;
00103   MatroskaDemux* fOurDemux;
00104   MatroskaParseState fCurrentParseState;
00105   u_int64_t fCurOffsetInFile, fSavedCurOffsetInFile, fLimitOffsetInFile;
00106 
00107   // For skipping over (possibly large) headers:
00108   u_int64_t fNumHeaderBytesToSkip;
00109 
00110   // For parsing 'Seek ID's:
00111   EBMLId fLastSeekId;
00112 
00113   // Parameters of the most recently-parsed 'Cluster':
00114   unsigned fClusterTimecode;
00115 
00116   // Parameters of the most recently-parsed 'Block':
00117   unsigned fBlockSize;
00118   unsigned fBlockTrackNumber;
00119   short fBlockTimecode;
00120   unsigned fNumFramesInBlock;
00121   unsigned* fFrameSizesWithinBlock;
00122 
00123   // Parameters of the most recently-parsed frame within a 'Block':
00124   double fPresentationTimeOffset;
00125   unsigned fNextFrameNumberToDeliver;
00126   unsigned fCurOffsetWithinFrame, fSavedCurOffsetWithinFrame; // used if track->haveSubframes()
00127 
00128   // Parameters of the (sub)frame that's currently being delivered:
00129   u_int8_t* fCurFrameTo;
00130   unsigned fCurFrameNumBytesToGet;
00131   unsigned fCurFrameNumBytesToSkip;
00132 };
00133 
00134 #endif

Generated on Tue Oct 28 14:12:28 2014 for live by  doxygen 1.5.2