Gnash  0.8.11dev
FLVParser.h
Go to the documentation of this file.
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.
10 //
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.
15 //
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
19 //
20 
21 
22 // Information about the FLV format can be found at http://osflash.org/flv
23 
24 #ifndef GNASH_FLVPARSER_H
25 #define GNASH_FLVPARSER_H
26 
27 #include "dsodefs.h"
28 #include "MediaParser.h" // for inheritance
29 #include "SimpleBuffer.h"
30 
31 #include <set>
32 #include <memory>
33 #include <map>
34 
35 #include <boost/thread/mutex.hpp>
36 
37 namespace gnash {
38 namespace media {
39 
41 //
45 {
46 public:
47 
49  //
58  ExtraVideoInfoFlv(boost::uint8_t* extradata, size_t datasize)
59  :
60  data(extradata),
61  size(datasize)
62  {
63  }
64 
66  boost::scoped_array<boost::uint8_t> data;
67 
69  size_t size;
70 };
71 
73 //
77 {
78 public:
79 
81  //
90  ExtraAudioInfoFlv(boost::uint8_t* extradata, size_t datasize)
91  :
92  data(extradata),
93  size(datasize)
94  {
95  }
96 
98  boost::scoped_array<boost::uint8_t> data;
99 
101  size_t size;
102 };
103 
106 {
107 
108 public:
109 
111  //
115  static const size_t paddingBytes = 8;
116 
120  //
125  FLVParser(std::auto_ptr<IOChannel> lt);
126 
128  ~FLVParser();
129 
130  // see dox in MediaParser.h
131  virtual bool seek(boost::uint32_t&);
132 
133  // see dox in MediaParser.h
134  virtual bool parseNextChunk();
135 
136  // see dox in MediaParser.h
137  boost::uint64_t getBytesLoaded() const;
138 
139  // see dox in MediaParser.h
140  bool indexingCompleted() const
141  {
142  return _indexingCompleted;
143  }
144 
146  //
151  //
156  //
157  virtual void fetchMetaTags(OrderedMetaTags& tags, boost::uint64_t ts);
158 
159 private:
160 
161  enum tagType
162  {
163  FLV_AUDIO_TAG = 0x08,
164  FLV_VIDEO_TAG = 0x09,
165  FLV_META_TAG = 0x12
166  };
167 
168  struct FLVTag : public boost::noncopyable
169  {
170  FLVTag(boost::uint8_t* stream)
171  :
172  type(stream[0]),
173  body_size(getUInt24(stream+1)),
174  timestamp(getUInt24(stream+4) | (stream[7] << 24) )
175  {}
176 
178  boost::uint8_t type;
179  boost::uint32_t body_size;
180  boost::uint32_t timestamp;
181  };
182 
183  struct FLVAudioTag : public boost::noncopyable
184  {
185  FLVAudioTag(const boost::uint8_t& byte)
186  :
187  codec( (byte & 0xf0) >> 4 ),
188  samplerate( flv_audio_rates[(byte & 0x0C) >> 2] ),
189  samplesize( 1 + ((byte & 0x02) >> 1)),
190  stereo( (byte & 0x01) )
191  {
192  }
193 
195  boost::uint8_t codec;
196 
197  boost::uint16_t samplerate;
198 
200  boost::uint8_t samplesize;
201 
202  bool stereo;
203 
204  private:
205 
206  static const boost::uint16_t flv_audio_rates[];
207 
208  };
209 
210  enum frameType
211  {
212  FLV_VIDEO_KEYFRAME = 1,
213  FLV_VIDEO_INTERLACED = 2,
214  FLV_VIDEO_DISPOSABLE = 3
215  };
216 
217  struct FLVVideoTag : public boost::noncopyable
218  {
219  FLVVideoTag(const boost::uint8_t& byte)
220  :
221  frametype( (byte & 0xf0) >> 4 ),
222  codec( byte & 0x0f )
223  {}
224 
226  boost::uint8_t frametype;
228  boost::uint8_t codec;
229  };
230 
232  //
236  bool parseNextTag(bool index_only);
237 
238  std::auto_ptr<EncodedAudioFrame> parseAudioTag(const FLVTag& flvtag,
239  const FLVAudioTag& audiotag, boost::uint32_t thisTagPos);
240 
241  std::auto_ptr<EncodedVideoFrame> parseVideoTag(const FLVTag& flvtag,
242  const FLVVideoTag& videotag, boost::uint32_t thisTagPos);
243 
244  void indexAudioTag(const FLVTag& tag, boost::uint32_t thisTagPos);
245 
246  void indexVideoTag(const FLVTag& tag, const FLVVideoTag& videotag,
247  boost::uint32_t thisTagPos);
248 
250  bool parseHeader();
251 
255  static boost::uint32_t getUInt24(boost::uint8_t* in);
256 
259  boost::uint64_t _lastParsedPosition;
260 
262  boost::uint64_t _nextPosToIndex;
263 
265  //
269  size_t _nextAudioFrame;
270 
272  //
276  size_t _nextVideoFrame;
277 
279  bool _audio;
280 
282  bool _video;
283 
284  std::auto_ptr<EncodedAudioFrame>
285  readAudioFrame(boost::uint32_t dataSize, boost::uint32_t timestamp);
286 
287  std::auto_ptr<EncodedVideoFrame>
288  readVideoFrame(boost::uint32_t dataSize, boost::uint32_t timestamp);
289 
293  typedef std::map<boost::uint64_t, long> CuePointsMap;
294  CuePointsMap _cuePoints;
295 
296  bool _indexingCompleted;
297 
298  MetaTags _metaTags;
299 
300  boost::mutex _metaTagsMutex;
301 };
302 
303 } // end of gnash::media namespace
304 } // end of gnash namespace
305 
306 #endif