1 // FLVParser.h: Flash Video file format parser, for Gnash.
3 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
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
28 #include "MediaParser.h" // for inheritance
29 #include "SimpleBuffer.h"
35 #include <boost/thread/mutex.hpp>
40 /// Extra video info found in some FLV embedded streams
42 /// This is basically composed by an header for the audio stream
44 class ExtraVideoInfoFlv
: public VideoInfo::ExtraInfo
48 /// Construct an ExtraVideoInfoFlv
51 /// Video header data. Ownership transferred.
54 /// Video header data size
56 /// @todo take a SimpleBuffer by auto_ptr
58 ExtraVideoInfoFlv(boost::uint8_t* extradata
, size_t datasize
)
65 /// Video stream header
66 boost::scoped_array
<boost::uint8_t> data
;
68 /// Video stream header size
72 /// Extra audoi info found in some FLV embedded streams
74 /// This is basically composed by an header for the audio stream
76 class ExtraAudioInfoFlv
: public AudioInfo::ExtraInfo
80 /// Construct an ExtraAudioInfoFlv
83 /// Audio header data. Ownership transferred.
86 /// Audio header data size
88 /// @todo take a SimpleBuffer by auto_ptr
90 ExtraAudioInfoFlv(boost::uint8_t* extradata
, size_t datasize
)
97 /// Audio stream header
98 boost::scoped_array
<boost::uint8_t> data
;
100 /// Audio stream header size
104 /// The FLVParser class parses FLV streams
105 class DSOEXPORT FLVParser
: public MediaParser
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;
118 /// Create an FLV parser reading input from
119 /// the given IOChannel
122 /// IOChannel to use for input.
123 /// Ownership transferred.
125 FLVParser(std::auto_ptr
<IOChannel
> lt
);
127 /// Kills the parser...
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
);
163 FLV_AUDIO_TAG
= 0x08,
164 FLV_VIDEO_TAG
= 0x09,
168 struct FLVTag
: public boost::noncopyable
170 FLVTag(boost::uint8_t* stream
)
173 body_size(getUInt24(stream
+1)),
174 timestamp(getUInt24(stream
+4) | (stream
[7] << 24) )
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
;
206 static const boost::uint16_t flv_audio_rates
[];
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 ),
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
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
281 /// Audio stream is present
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
292 /// second: position in input stream
293 typedef std::map
<boost::uint64_t, long> CuePointsMap
;
294 CuePointsMap _cuePoints
;
296 bool _indexingCompleted
;
300 boost::mutex _metaTagsMutex
;
303 } // end of gnash::media namespace
304 } // end of gnash namespace