Gnash 0.8.9
|
00001 // FLVParser.h: Flash Video file format parser, for Gnash. 00002 // 00003 // Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. 00004 // 00005 // This program is free software; you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation; either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // This program is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with this program; if not, write to the Free Software 00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 // 00019 00020 00021 // Information about the FLV format can be found at http://osflash.org/flv 00022 00023 #ifndef GNASH_FLVPARSER_H 00024 #define GNASH_FLVPARSER_H 00025 00026 #include "dsodefs.h" 00027 #include "MediaParser.h" // for inheritance 00028 #include "SimpleBuffer.h" 00029 00030 #include <set> 00031 #include <memory> 00032 #include <map> 00033 00034 #include <boost/thread/mutex.hpp> 00035 00036 namespace gnash { 00037 namespace media { 00038 00040 // 00043 class ExtraVideoInfoFlv : public VideoInfo::ExtraInfo 00044 { 00045 public: 00046 00048 // 00057 ExtraVideoInfoFlv(boost::uint8_t* extradata, size_t datasize) 00058 : 00059 data(extradata), 00060 size(datasize) 00061 { 00062 } 00063 00065 boost::scoped_array<boost::uint8_t> data; 00066 00068 size_t size; 00069 }; 00070 00072 // 00075 class ExtraAudioInfoFlv : public AudioInfo::ExtraInfo 00076 { 00077 public: 00078 00080 // 00089 ExtraAudioInfoFlv(boost::uint8_t* extradata, size_t datasize) 00090 : 00091 data(extradata), 00092 size(datasize) 00093 { 00094 } 00095 00097 boost::scoped_array<boost::uint8_t> data; 00098 00100 size_t size; 00101 }; 00102 00104 class DSOEXPORT FLVParser : public MediaParser 00105 { 00106 00107 public: 00108 00110 // 00114 static const size_t paddingBytes = 8; 00115 00119 // 00124 FLVParser(std::auto_ptr<IOChannel> lt); 00125 00127 ~FLVParser(); 00128 00129 // see dox in MediaParser.h 00130 virtual bool seek(boost::uint32_t&); 00131 00132 // see dox in MediaParser.h 00133 virtual bool parseNextChunk(); 00134 00135 // see dox in MediaParser.h 00136 boost::uint64_t getBytesLoaded() const; 00137 00138 // see dox in MediaParser.h 00139 bool indexingCompleted() const 00140 { 00141 return _indexingCompleted; 00142 } 00143 00145 // 00150 // 00155 // 00156 virtual void fetchMetaTags(OrderedMetaTags& tags, boost::uint64_t ts); 00157 00158 private: 00159 00160 enum tagType 00161 { 00162 FLV_AUDIO_TAG = 0x08, 00163 FLV_VIDEO_TAG = 0x09, 00164 FLV_META_TAG = 0x12 00165 }; 00166 00167 struct FLVTag : public boost::noncopyable 00168 { 00169 FLVTag(boost::uint8_t* stream) 00170 : 00171 type(stream[0]), 00172 body_size(getUInt24(stream+1)), 00173 timestamp(getUInt24(stream+4) | (stream[7] << 24) ) 00174 {} 00175 00177 boost::uint8_t type; 00178 boost::uint32_t body_size; 00179 boost::uint32_t timestamp; 00180 }; 00181 00182 struct FLVAudioTag : public boost::noncopyable 00183 { 00184 FLVAudioTag(const boost::uint8_t& byte) 00185 : 00186 codec( (byte & 0xf0) >> 4 ), 00187 samplerate( flv_audio_rates[(byte & 0x0C) >> 2] ), 00188 samplesize( 1 + ((byte & 0x02) >> 1)), 00189 stereo( (byte & 0x01) ) 00190 { 00191 } 00192 00194 boost::uint8_t codec; 00195 00196 boost::uint16_t samplerate; 00197 00199 boost::uint8_t samplesize; 00200 00201 bool stereo; 00202 00203 private: 00204 00205 static const boost::uint16_t flv_audio_rates[]; 00206 00207 }; 00208 00209 enum frameType 00210 { 00211 FLV_VIDEO_KEYFRAME = 1, 00212 FLV_VIDEO_INTERLACED = 2, 00213 FLV_VIDEO_DISPOSABLE = 3 00214 }; 00215 00216 struct FLVVideoTag : public boost::noncopyable 00217 { 00218 FLVVideoTag(const boost::uint8_t& byte) 00219 : 00220 frametype( (byte & 0xf0) >> 4 ), 00221 codec( byte & 0x0f ) 00222 {} 00223 00225 boost::uint8_t frametype; 00227 boost::uint8_t codec; 00228 }; 00229 00231 // 00235 bool parseNextTag(bool index_only); 00236 00237 std::auto_ptr<EncodedAudioFrame> parseAudioTag(const FLVTag& flvtag, 00238 const FLVAudioTag& audiotag, boost::uint32_t thisTagPos); 00239 00240 std::auto_ptr<EncodedVideoFrame> parseVideoTag(const FLVTag& flvtag, 00241 const FLVVideoTag& videotag, boost::uint32_t thisTagPos); 00242 00243 void indexAudioTag(const FLVTag& tag, boost::uint32_t thisTagPos); 00244 00245 void indexVideoTag(const FLVTag& tag, const FLVVideoTag& videotag, 00246 boost::uint32_t thisTagPos); 00247 00249 bool parseHeader(); 00250 00254 static boost::uint32_t getUInt24(boost::uint8_t* in); 00255 00258 boost::uint64_t _lastParsedPosition; 00259 00261 boost::uint64_t _nextPosToIndex; 00262 00264 // 00268 size_t _nextAudioFrame; 00269 00271 // 00275 size_t _nextVideoFrame; 00276 00278 bool _audio; 00279 00281 bool _video; 00282 00283 std::auto_ptr<EncodedAudioFrame> 00284 readAudioFrame(boost::uint32_t dataSize, boost::uint32_t timestamp); 00285 00286 std::auto_ptr<EncodedVideoFrame> 00287 readVideoFrame(boost::uint32_t dataSize, boost::uint32_t timestamp); 00288 00292 typedef std::map<boost::uint64_t, long> CuePointsMap; 00293 CuePointsMap _cuePoints; 00294 00295 bool _indexingCompleted; 00296 00297 MetaTags _metaTags; 00298 00299 boost::mutex _metaTagsMutex; 00300 }; 00301 00302 } // end of gnash::media namespace 00303 } // end of gnash namespace 00304 00305 #endif