install-man1 doesn't need to depend on EXTRAMANPAGES
[gnash.git] / libmedia / FLVParser.h
blobd302bb151edb0571c4dba888d6171463090db358
1 // FLVParser.h: Flash Video file format parser, for Gnash.
2 //
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 // Information about the FLV format can be found at http://osflash.org/flv
24 #ifndef GNASH_FLVPARSER_H
25 #define GNASH_FLVPARSER_H
27 #include "dsodefs.h"
28 #include "MediaParser.h" // for inheritance
29 #include "SimpleBuffer.h"
31 #include <set>
32 #include <memory>
33 #include <map>
35 #include <boost/thread/mutex.hpp>
37 namespace gnash {
38 namespace media {
40 /// Extra video info found in some FLV embedded streams
42 /// This is basically composed by an header for the audio stream
43 ///
44 class ExtraVideoInfoFlv : public VideoInfo::ExtraInfo
46 public:
48 /// Construct an ExtraVideoInfoFlv
50 /// @param extradata
51 /// Video header data. Ownership transferred.
52 ///
53 /// @param datasize
54 /// Video header data size
55 ///
56 /// @todo take a SimpleBuffer by auto_ptr
57 ///
58 ExtraVideoInfoFlv(boost::uint8_t* extradata, size_t datasize)
60 data(extradata),
61 size(datasize)
65 /// Video stream header
66 boost::scoped_array<boost::uint8_t> data;
68 /// Video stream header size
69 size_t size;
72 /// Extra audoi info found in some FLV embedded streams
74 /// This is basically composed by an header for the audio stream
75 ///
76 class ExtraAudioInfoFlv : public AudioInfo::ExtraInfo
78 public:
80 /// Construct an ExtraAudioInfoFlv
82 /// @param extradata
83 /// Audio header data. Ownership transferred.
84 ///
85 /// @param datasize
86 /// Audio header data size
87 ///
88 /// @todo take a SimpleBuffer by auto_ptr
89 ///
90 ExtraAudioInfoFlv(boost::uint8_t* extradata, size_t datasize)
92 data(extradata),
93 size(datasize)
97 /// Audio stream header
98 boost::scoped_array<boost::uint8_t> data;
100 /// Audio stream header size
101 size_t size;
104 /// The FLVParser class parses FLV streams
105 class DSOEXPORT FLVParser : public MediaParser
108 public:
110 /// The size of padding for all buffers that might be read by FFMPEG.
112 /// This possibly also applies to other media handlers (gst).
113 /// Ideally this would be taken from the current Handler, but we don't
114 /// know about it here.
115 static const size_t paddingBytes = 8;
117 /// \brief
118 /// Create an FLV parser reading input from
119 /// the given IOChannel
121 /// @param lt
122 /// IOChannel to use for input.
123 /// Ownership transferred.
125 FLVParser(std::auto_ptr<IOChannel> lt);
127 /// Kills the parser...
128 ~FLVParser();
130 // see dox in MediaParser.h
131 virtual bool seek(boost::uint32_t&);
133 // see dox in MediaParser.h
134 virtual bool parseNextChunk();
136 // see dox in MediaParser.h
137 boost::uint64_t getBytesLoaded() const;
139 // see dox in MediaParser.h
140 bool indexingCompleted() const
142 return _indexingCompleted;
145 /// Retrieve any parsed metadata tags up to a specified timestamp.
147 /// This copies pointers to a SimpleBuffer of AMF data from _metaTags,
148 /// then removes those pointers from the MetaTags map. Any metadata later
149 /// than the timestamp is kept until fetchMetaTags is called again (or
150 /// the dtor is called).
152 /// @param ts The latest timestamp to retrieve metadata for.
153 /// @param tags This is filled with shared pointers to metatags in
154 /// timestamp order. Ownership of the data is shared. It
155 /// is destroyed automatically along with the last owner.
157 virtual void fetchMetaTags(OrderedMetaTags& tags, boost::uint64_t ts);
159 private:
161 enum tagType
163 FLV_AUDIO_TAG = 0x08,
164 FLV_VIDEO_TAG = 0x09,
165 FLV_META_TAG = 0x12
168 struct FLVTag : public boost::noncopyable
170 FLVTag(boost::uint8_t* stream)
172 type(stream[0]),
173 body_size(getUInt24(stream+1)),
174 timestamp(getUInt24(stream+4) | (stream[7] << 24) )
177 /// Equals tagType
178 boost::uint8_t type;
179 boost::uint32_t body_size;
180 boost::uint32_t timestamp;
183 struct FLVAudioTag : public boost::noncopyable
185 FLVAudioTag(const boost::uint8_t& byte)
187 codec( (byte & 0xf0) >> 4 ),
188 samplerate( flv_audio_rates[(byte & 0x0C) >> 2] ),
189 samplesize( 1 + ((byte & 0x02) >> 1)),
190 stereo( (byte & 0x01) )
194 /// Equals audioCodecType
195 boost::uint8_t codec;
197 boost::uint16_t samplerate;
199 /// Size of each sample, in bytes
200 boost::uint8_t samplesize;
202 bool stereo;
204 private:
206 static const boost::uint16_t flv_audio_rates[];
210 enum frameType
212 FLV_VIDEO_KEYFRAME = 1,
213 FLV_VIDEO_INTERLACED = 2,
214 FLV_VIDEO_DISPOSABLE = 3
217 struct FLVVideoTag : public boost::noncopyable
219 FLVVideoTag(const boost::uint8_t& byte)
221 frametype( (byte & 0xf0) >> 4 ),
222 codec( byte & 0x0f )
225 /// Equals frameType
226 boost::uint8_t frametype;
227 /// Equals videoCodecType
228 boost::uint8_t codec;
231 /// Parses next tag from the file
233 /// Returns true if something was parsed, false otherwise.
234 /// Sets _parsingComplete=true on end of file.
236 bool parseNextTag(bool index_only);
238 std::auto_ptr<EncodedAudioFrame> parseAudioTag(const FLVTag& flvtag,
239 const FLVAudioTag& audiotag, boost::uint32_t thisTagPos);
241 std::auto_ptr<EncodedVideoFrame> parseVideoTag(const FLVTag& flvtag,
242 const FLVVideoTag& videotag, boost::uint32_t thisTagPos);
244 void indexAudioTag(const FLVTag& tag, boost::uint32_t thisTagPos);
246 void indexVideoTag(const FLVTag& tag, const FLVVideoTag& videotag,
247 boost::uint32_t thisTagPos);
249 /// Parses the header of the file
250 bool parseHeader();
252 /// Reads three bytes in FLV (big endian) byte order.
253 /// @param in Pointer to read 3 bytes from.
254 /// @return 24-bit integer.
255 static boost::uint32_t getUInt24(boost::uint8_t* in);
257 /// The position where the parsing should continue from.
258 /// Will be reset on seek, and will be protected by the _streamMutex
259 boost::uint64_t _lastParsedPosition;
261 /// Position of next tag to index
262 boost::uint64_t _nextPosToIndex;
264 /// Audio frame cursor position
266 /// This is the video frame number that will
267 /// be referenced by nextVideoFrame and nextVideoFrameTimestamp
269 size_t _nextAudioFrame;
271 /// Video frame cursor position
273 /// This is the video frame number that will
274 /// be referenced by nextVideoFrame and nextVideoFrameTimestamp
276 size_t _nextVideoFrame;
278 /// Audio stream is present
279 bool _audio;
281 /// Audio stream is present
282 bool _video;
284 std::auto_ptr<EncodedAudioFrame>
285 readAudioFrame(boost::uint32_t dataSize, boost::uint32_t timestamp);
287 std::auto_ptr<EncodedVideoFrame>
288 readVideoFrame(boost::uint32_t dataSize, boost::uint32_t timestamp);
290 /// Position in input stream for each cue point
291 /// first: timestamp
292 /// second: position in input stream
293 typedef std::map<boost::uint64_t, long> CuePointsMap;
294 CuePointsMap _cuePoints;
296 bool _indexingCompleted;
298 MetaTags _metaTags;
300 boost::mutex _metaTagsMutex;
303 } // end of gnash::media namespace
304 } // end of gnash namespace
306 #endif