liveMedia/H263plusVideoStreamParser.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-2012 Live Networks, Inc.  All rights reserved.
00018 // A filter that breaks up an H263 video stream into frames.
00019 // derived from MPEG4IP h263.c
00020 // Author Benhard Feiten
00021 
00022 #ifndef _H263PLUS_VIDEO_STREAM_PARSER_HH
00023 #define _H263PLUS_VIDEO_STREAM_PARSER_HH
00024 
00025 #ifndef _STREAM_PARSER_HH
00026 #include "StreamParser.hh"
00027 #endif
00028 
00029 
00030 // Default timescale for H.263 (1000ms)
00031 #define H263_TIMESCALE 1000
00032 
00033 // Default H263 frame rate (30fps)
00034 #define H263_BASIC_FRAME_RATE 30
00035 
00036 // Minimum number of bytes needed to parse an H263 header
00037 #define H263_REQUIRE_HEADER_SIZE_BYTES 5
00038 
00039 // Number of bytes the start code requries
00040 #define H263_STARTCODE_SIZE_BYTES 3
00041 
00042 // This is the input buffer's size. It should contain
00043 // 1 frame with the following start code
00044 #define H263_BUFFER_SIZE 256 * 1024
00045 
00046 //  additionalBytesNeeded - indicates how many additional bytes are to be read
00047 //  from the next frame's header (over the 3 bytes that are already read).
00048 #define ADDITIONAL_BYTES_NEEDED H263_REQUIRE_HEADER_SIZE_BYTES - H263_STARTCODE_SIZE_BYTES
00049 
00050 // The default max different (in %) betwqeen max and average bitrates
00051 #define H263_DEFAULT_CBR_TOLERANCE  10
00052 
00053 
00054 
00055 // The following structure holds information extracted from each frame's header:
00056 typedef struct _H263INFO {
00057   u_int8_t  tr;                 // Temporal Reference, used in duration calculation
00058   u_int16_t width;              // Width of the picture
00059   u_int16_t height;             // Height of the picture
00060   bool      isSyncFrame;        // Frame type (true = I frame = "sync" frame)
00061 } H263INFO;
00062 
00063 typedef struct _MaxBitrate_CTX {
00064   u_int32_t  bitrateTable[H263_BASIC_FRAME_RATE];// Window of 1 second
00065   u_int32_t  windowBitrate;              // The bitrate of the current window
00066   u_int32_t  maxBitrate;                 // The up-to-date maximum bitrate
00067   u_int32_t  tableIndex;                 // The next TR unit to update
00068 } MaxBitrate_CTX;
00069 
00070 
00071 class H263plusVideoStreamParser : public StreamParser {
00072 
00073 public:
00074    H263plusVideoStreamParser( class H263plusVideoStreamFramer* usingSource,
00075                                           FramedSource* inputSource);
00076 
00077    virtual ~H263plusVideoStreamParser();
00078 
00079    void registerReadInterest(unsigned char* to, unsigned maxSize);
00080 
00081    unsigned parse(u_int64_t & currentDuration);    // returns the size of the frame  that was acquired, or 0 if none
00082    unsigned numTruncatedBytes() const { return fNumTruncatedBytes; }  // The number of truncated bytes (if any)
00083 
00084 
00085 protected:
00086 //  H263plusVideoStreamFramer* usingSource() {
00087 //    return (H263plusVideoStreamFramer*)fUsingSource;
00088 //  }
00089   void setParseState();
00090 
00091 //   void setParseState(H263plusParseState parseState);
00092 
00093 
00094 private:
00095    int       parseH263Frame( );
00096    bool      ParseShortHeader(u_int8_t *headerBuffer, H263INFO *outputInfoStruct);
00097    void      GetMaxBitrate( MaxBitrate_CTX *ctx, u_int32_t frameSize, u_int8_t frameTRDiff);
00098    u_int64_t CalculateDuration(u_int8_t trDiff);
00099    bool      GetWidthAndHeight( u_int8_t  fmt, u_int16_t *width, u_int16_t *height);
00100    u_int8_t  GetTRDifference( u_int8_t nextTR, u_int8_t currentTR);
00101 
00102   virtual void restoreSavedParserState();
00103 
00104 protected:
00105    class H263plusVideoStreamFramer* fUsingSource;
00106 
00107    unsigned char* fTo;
00108    unsigned       fMaxSize;
00109    unsigned char* fStartOfFrame;
00110    unsigned char* fSavedTo;
00111    unsigned char* fLimit;
00112    unsigned       fNumTruncatedBytes;
00113    unsigned       fSavedNumTruncatedBytes;
00114 
00115 private:
00116    H263INFO       fNextInfo;       // Holds information about the next frame
00117    H263INFO       fCurrentInfo;    // Holds information about the current frame
00118    MaxBitrate_CTX fMaxBitrateCtx;  // Context for the GetMaxBitrate function
00119    char           fStates[3][256];
00120    u_int8_t       fNextHeader[H263_REQUIRE_HEADER_SIZE_BYTES];
00121 
00122   u_int32_t fnextTR;   // The next frame's presentation time in TR units
00123   u_int64_t fcurrentPT;   // The current frame's presentation time in milli-seconds
00124 
00125 };
00126 
00127 #endif

Generated on Thu May 10 20:25:11 2012 for live by  doxygen 1.5.2