| 1 | /********** |
| 2 | This library is free software; you can redistribute it and/or modify it under |
| 3 | the terms of the GNU Lesser General Public License as published by the |
| 4 | Free Software Foundation; either version 3 of the License, or (at your |
| 5 | option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.) |
| 6 | |
| 7 | This library is distributed in the hope that it will be useful, but WITHOUT |
| 8 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 9 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
| 10 | more details. |
| 11 | |
| 12 | You should have received a copy of the GNU Lesser General Public License |
| 13 | along with this library; if not, write to the Free Software Foundation, Inc., |
| 14 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 15 | **********/ |
| 16 | // "liveMedia" |
| 17 | // Copyright (c) 1996-2020 Live Networks, Inc. All rights reserved. |
| 18 | // A filter that breaks up an H263 video stream into frames. |
| 19 | // derived from MPEG4IP h263.c |
| 20 | // Author Benhard Feiten |
| 21 | |
| 22 | #ifndef _H263PLUS_VIDEO_STREAM_PARSER_HH |
| 23 | #define _H263PLUS_VIDEO_STREAM_PARSER_HH |
| 24 | |
| 25 | #ifndef _STREAM_PARSER_HH |
| 26 | #include "StreamParser.hh" |
| 27 | #endif |
| 28 | |
| 29 | |
| 30 | // Default timescale for H.263 (1000ms) |
| 31 | #define H263_TIMESCALE 1000 |
| 32 | |
| 33 | // Default H263 frame rate (30fps) |
| 34 | #define H263_BASIC_FRAME_RATE 30 |
| 35 | |
| 36 | // Minimum number of bytes needed to parse an H263 header |
| 37 | #define 5 |
| 38 | |
| 39 | // Number of bytes the start code requries |
| 40 | #define H263_STARTCODE_SIZE_BYTES 3 |
| 41 | |
| 42 | // This is the input buffer's size. It should contain |
| 43 | // 1 frame with the following start code |
| 44 | #define H263_BUFFER_SIZE 256 * 1024 |
| 45 | |
| 46 | // additionalBytesNeeded - indicates how many additional bytes are to be read |
| 47 | // from the next frame's header (over the 3 bytes that are already read). |
| 48 | #define ADDITIONAL_BYTES_NEEDED H263_REQUIRE_HEADER_SIZE_BYTES - H263_STARTCODE_SIZE_BYTES |
| 49 | |
| 50 | // The default max different (in %) betwqeen max and average bitrates |
| 51 | #define H263_DEFAULT_CBR_TOLERANCE 10 |
| 52 | |
| 53 | |
| 54 | |
| 55 | // The following structure holds information extracted from each frame's header: |
| 56 | typedef struct _H263INFO { |
| 57 | u_int8_t tr; // Temporal Reference, used in duration calculation |
| 58 | u_int16_t width; // Width of the picture |
| 59 | u_int16_t height; // Height of the picture |
| 60 | bool isSyncFrame; // Frame type (true = I frame = "sync" frame) |
| 61 | } H263INFO; |
| 62 | |
| 63 | typedef struct _MaxBitrate_CTX { |
| 64 | u_int32_t bitrateTable[H263_BASIC_FRAME_RATE];// Window of 1 second |
| 65 | u_int32_t windowBitrate; // The bitrate of the current window |
| 66 | u_int32_t maxBitrate; // The up-to-date maximum bitrate |
| 67 | u_int32_t tableIndex; // The next TR unit to update |
| 68 | } MaxBitrate_CTX; |
| 69 | |
| 70 | |
| 71 | class H263plusVideoStreamParser : public StreamParser { |
| 72 | |
| 73 | public: |
| 74 | H263plusVideoStreamParser( class H263plusVideoStreamFramer* usingSource, |
| 75 | FramedSource* inputSource); |
| 76 | |
| 77 | virtual ~H263plusVideoStreamParser(); |
| 78 | |
| 79 | void registerReadInterest(unsigned char* to, unsigned maxSize); |
| 80 | |
| 81 | unsigned parse(u_int64_t & currentDuration); // returns the size of the frame that was acquired, or 0 if none |
| 82 | unsigned numTruncatedBytes() const { return fNumTruncatedBytes; } // The number of truncated bytes (if any) |
| 83 | |
| 84 | |
| 85 | protected: |
| 86 | // H263plusVideoStreamFramer* usingSource() { |
| 87 | // return (H263plusVideoStreamFramer*)fUsingSource; |
| 88 | // } |
| 89 | void setParseState(); |
| 90 | |
| 91 | // void setParseState(H263plusParseState parseState); |
| 92 | |
| 93 | |
| 94 | private: |
| 95 | int parseH263Frame( ); |
| 96 | bool (u_int8_t *, H263INFO *outputInfoStruct); |
| 97 | void GetMaxBitrate( MaxBitrate_CTX *ctx, u_int32_t frameSize, u_int8_t frameTRDiff); |
| 98 | u_int64_t CalculateDuration(u_int8_t trDiff); |
| 99 | bool GetWidthAndHeight( u_int8_t fmt, u_int16_t *width, u_int16_t *height); |
| 100 | u_int8_t GetTRDifference( u_int8_t nextTR, u_int8_t currentTR); |
| 101 | |
| 102 | virtual void restoreSavedParserState(); |
| 103 | |
| 104 | protected: |
| 105 | class H263plusVideoStreamFramer* fUsingSource; |
| 106 | |
| 107 | unsigned char* fTo; |
| 108 | unsigned fMaxSize; |
| 109 | unsigned char* fStartOfFrame; |
| 110 | unsigned char* fSavedTo; |
| 111 | unsigned char* fLimit; |
| 112 | unsigned fNumTruncatedBytes; |
| 113 | unsigned fSavedNumTruncatedBytes; |
| 114 | |
| 115 | private: |
| 116 | H263INFO fNextInfo; // Holds information about the next frame |
| 117 | H263INFO fCurrentInfo; // Holds information about the current frame |
| 118 | MaxBitrate_CTX fMaxBitrateCtx; // Context for the GetMaxBitrate function |
| 119 | char fStates[3][256]; |
| 120 | u_int8_t [H263_REQUIRE_HEADER_SIZE_BYTES]; |
| 121 | |
| 122 | u_int32_t fnextTR; // The next frame's presentation time in TR units |
| 123 | u_int64_t fcurrentPT; // The current frame's presentation time in milli-seconds |
| 124 | |
| 125 | }; |
| 126 | |
| 127 | #endif |
| 128 | |