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 <memory>
28 #include <map>
29 #include <mutex>
30 
31 #include <boost/utility.hpp> // noncopyable
32 
33 #include "dsodefs.h"
34 #include "MediaParser.h" // for inheritance
35 
36 namespace gnash {
37 namespace media {
38 
40 //
44 {
45 public:
46 
48  //
57  ExtraVideoInfoFlv(std::uint8_t* extradata, size_t datasize)
58  :
59  data(extradata),
60  size(datasize)
61  {
62  }
63 
65  std::unique_ptr<std::uint8_t[]> data;
66 
68  size_t size;
69 };
70 
72 //
76 {
77 public:
78 
80  //
89  ExtraAudioInfoFlv(std::uint8_t* extradata, size_t datasize)
90  :
91  data(extradata),
92  size(datasize)
93  {
94  }
95 
97  std::unique_ptr<std::uint8_t[]> data;
98 
100  size_t size;
101 };
102 
105 {
106 
107 public:
108 
110  //
114  static const size_t paddingBytes = 8;
115 
119  //
124  FLVParser(std::unique_ptr<IOChannel> lt);
125 
127  ~FLVParser();
128 
129  // see dox in MediaParser.h
130  virtual bool seek(std::uint32_t&);
131 
132  // see dox in MediaParser.h
133  virtual bool parseNextChunk();
134 
135  // see dox in MediaParser.h
136  std::uint64_t getBytesLoaded() const;
137 
138  // see dox in MediaParser.h
139  bool indexingCompleted() const
140  {
141  return _indexingCompleted;
142  }
143 
145  //
150  //
155  //
156  virtual void fetchMetaTags(OrderedMetaTags& tags, std::uint64_t ts);
157 
158 private:
159 
160  enum tagType
161  {
162  FLV_AUDIO_TAG = 0x08,
163  FLV_VIDEO_TAG = 0x09,
164  FLV_META_TAG = 0x12
165  };
166 
167  struct FLVTag : public boost::noncopyable
168  {
169  FLVTag(std::uint8_t* stream)
170  :
171  type(stream[0]),
172  body_size(getUInt24(stream+1)),
173  timestamp(getUInt24(stream+4) | (stream[7] << 24) )
174  {}
175 
177  std::uint8_t type;
178  std::uint32_t body_size;
179  std::uint32_t timestamp;
180  };
181 
182  struct FLVAudioTag : public boost::noncopyable
183  {
184  FLVAudioTag(const std::uint8_t& byte)
185  :
186  codec( (byte & 0xf0) >> 4 ),
187  samplerate( flv_audio_rates[(byte & 0x0C) >> 2] ),
188  samplesize( 1 + ((byte & 0x02) >> 1)),
189  stereo( (byte & 0x01) )
190  {
191  }
192 
194  std::uint8_t codec;
195 
196  std::uint16_t samplerate;
197 
199  std::uint8_t samplesize;
200 
201  bool stereo;
202 
203  private:
204 
205  static const std::uint16_t flv_audio_rates[];
206 
207  };
208 
209  enum frameType
210  {
211  FLV_VIDEO_KEYFRAME = 1,
212  FLV_VIDEO_INTERLACED = 2,
213  FLV_VIDEO_DISPOSABLE = 3
214  };
215 
216  struct FLVVideoTag : public boost::noncopyable
217  {
218  FLVVideoTag(const std::uint8_t& byte)
219  :
220  frametype( (byte & 0xf0) >> 4 ),
221  codec( byte & 0x0f )
222  {}
223 
225  std::uint8_t frametype;
227  std::uint8_t codec;
228  };
229 
231  //
235  bool parseNextTag(bool index_only);
236 
237  std::unique_ptr<EncodedAudioFrame> parseAudioTag(const FLVTag& flvtag,
238  const FLVAudioTag& audiotag, std::uint32_t thisTagPos);
239 
240  std::unique_ptr<EncodedVideoFrame> parseVideoTag(const FLVTag& flvtag,
241  const FLVVideoTag& videotag, std::uint32_t thisTagPos);
242 
243  void indexAudioTag(const FLVTag& tag, std::uint32_t thisTagPos);
244 
245  void indexVideoTag(const FLVTag& tag, const FLVVideoTag& videotag,
246  std::uint32_t thisTagPos);
247 
249  bool parseHeader();
250 
254  static std::uint32_t getUInt24(std::uint8_t* in);
255 
258  std::uint64_t _lastParsedPosition;
259 
261  std::uint64_t _nextPosToIndex;
262 
264  bool _audio;
265 
267  bool _video;
268 
269  std::unique_ptr<EncodedAudioFrame>
270  readAudioFrame(std::uint32_t dataSize, std::uint32_t timestamp);
271 
272  std::unique_ptr<EncodedVideoFrame>
273  readVideoFrame(std::uint32_t dataSize, std::uint32_t timestamp);
274 
278  typedef std::map<std::uint64_t, long> CuePointsMap;
279  CuePointsMap _cuePoints;
280 
281  bool _indexingCompleted;
282 
283  MetaTags _metaTags;
284 
285  std::mutex _metaTagsMutex;
286 };
287 
288 } // end of gnash::media namespace
289 } // end of gnash namespace
290 
291 #endif
gnash::LogFile::getDefaultInstance
static LogFile & getDefaultInstance()
Definition: log.cpp:77
position
@ position
Definition: klash_part.cpp:329
Arg_parser::Option
Definition: arg_parser.h:59
gnash::media::FLVParser::fetchMetaTags
virtual void fetchMetaTags(OrderedMetaTags &tags, std::uint64_t ts)
Retrieve any parsed metadata tags up to a specified timestamp.
Definition: FLVParser.cpp:563
gnash::media::FLVParser::seek
virtual bool seek(std::uint32_t &)
Seeks to the closest possible position the given position, and returns the new position.
Definition: FLVParser.cpp:69
Arg_parser::argument
std::string argument(const int i) const
Definition: arg_parser.h:119
SimpleBuffer.h
gnash::media::MediaParser::_audioInfo
std::unique_ptr< AudioInfo > _audioInfo
Info about the audio stream (if any)
Definition: MediaParser.h:659
dbglogfile
#define dbglogfile
Definition: gtkext.cpp:48
dsodefs.h
gnash::log_debug
void log_debug(StringType msg, Args... args)
Definition: log.h:301
gnash::key::i
@ i
Definition: GnashKey.h:155
Arg_parser::arguments
int arguments() const
Definition: arg_parser.h:109
gnash::LogFile
Definition: log.h:64
gnash::media::MediaParser::_parsingComplete
bool _parsingComplete
Whether the parsing is complete or not.
Definition: MediaParser.h:662
gnash::media::MediaParser::startParserThread
void startParserThread()
}@
Definition: MediaParser.cpp:50
Arg_parser::ArgParserException
Definition: arg_parser.h:66
rc.h
_
#define _(String)
Definition: log.h:44
gnash::media::VIDEO_CODEC_VP6A
@ VIDEO_CODEC_VP6A
On2 VP6 Alpha video codec.
Definition: MediaParser.h:90
gnash
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:41
gnash::media::MediaParser
The MediaParser class provides cursor-based access to encoded media frames.
Definition: MediaParser.h:473
boost
Definition: gui.h:74
PACKAGE
#define PACKAGE
Definition: gnashconfig.h:556
utility.h
gnash::media::FLVParser::paddingBytes
static const size_t paddingBytes
The size of padding for all buffers that might be read by FFMPEG.
Definition: FLVParser.h:114
gnash::media::ExtraAudioInfoFlv
Extra audoi info found in some FLV embedded streams.
Definition: FLVParser.h:76
gnash::media::EncodedAudioFrame
An encoded audio frame.
Definition: MediaParser.h:454
gnash::media::MediaParser::_bytesLoaded
std::atomic< std::uint_fast64_t > _bytesLoaded
Number of bytes loaded.
Definition: MediaParser.h:665
ts
std::uint32_t ts
Definition: LocalConnection_as.cpp:150
gnash::media::ExtraAudioInfoFlv::data
std::unique_ptr< std::uint8_t[]> data
Audio stream header.
Definition: FLVParser.h:97
gnash::media::VIDEO_CODEC_H264
@ VIDEO_CODEC_H264
MPEG-4 Part 10, or Advanced Video Coding.
Definition: MediaParser.h:96
gnash::log_error
void log_error(StringType msg, Args... args)
Definition: log.h:283
Arg_parser::error
const std::string & error() const
Definition: arg_parser.h:106
Arg_parser::no
@ no
Definition: arg_parser.h:56
Arg_parser
Definition: arg_parser.h:54
bindtextdomain
#define bindtextdomain(Domainname, Dirname)
Definition: gettext.h:66
gnash::media::MediaParser::pushEncodedVideoFrame
void pushEncodedVideoFrame(std::unique_ptr< EncodedVideoFrame > frame)
Push an encoded video frame to buffer.
Definition: MediaParser.cpp:339
gnash::media::ExtraVideoInfoFlv::ExtraVideoInfoFlv
ExtraVideoInfoFlv(std::uint8_t *extradata, size_t datasize)
Construct an ExtraVideoInfoFlv.
Definition: FLVParser.h:57
fill
VGPaint fill
Definition: testr_gtk.cpp:86
GnashFileUtilities.h
gnash::media::ExtraVideoInfoFlv::size
size_t size
Video stream header size.
Definition: FLVParser.h:68
textdomain
#define textdomain(Domainname)
Definition: gettext.h:65
GnashSystemNetHeaders.h
FLVParser.h
gnash::media::ExtraVideoInfoFlv::data
std::unique_ptr< std::uint8_t[]> data
Video stream header.
Definition: FLVParser.h:65
gnash::media::VIDEO_CODEC_VP6
@ VIDEO_CODEC_VP6
On2 VP6 video codec.
Definition: MediaParser.h:87
main
int main(int argc, char *argv[])
Definition: flvdumper.cpp:116
gnash::key::type
type
Definition: GnashKey.h:330
GnashAlgorithm.h
gnash::key::code
code
Definition: GnashKey.h:44
gnash::media::MediaParser::_stream
std::unique_ptr< IOChannel > _stream
The stream used to access the file.
Definition: MediaParser.h:697
gnash::media::FLVParser::getBytesLoaded
std::uint64_t getBytesLoaded() const
Return number of bytes parsed so far.
Definition: FLVParser.cpp:507
infiles
std::vector< std::string > infiles
Definition: gnash.cpp:58
gnash::SimpleBuffer::size
size_t size() const
Return size of the buffer.
Definition: SimpleBuffer.h:75
gnash::media::VideoInfo::ExtraInfo
Extra info about a video stream.
Definition: MediaParser.h:379
gnash::media::MediaParser::bufferFull
bool bufferFull() const
Method to check if buffer is full w/out locking the _qMutex.
Definition: MediaParser.cpp:396
gnash::media::AUDIO_CODEC_AAC
@ AUDIO_CODEC_AAC
Advanced Audio Coding.
Definition: MediaParser.h:184
MediaParser.h
gnash::media::MediaParser::_videoInfo
std::unique_ptr< VideoInfo > _videoInfo
Subclasses must set the following variables:
Definition: MediaParser.h:656
gnash::media::CODEC_TYPE_FLASH
@ CODEC_TYPE_FLASH
The internal flash codec ids.
Definition: MediaParser.h:68
gnash::media::FLVParser::parseNextChunk
virtual bool parseNextChunk()
Parse next chunk of input.
Definition: FLVParser.cpp:123
gnash::media::MediaParser::clearBuffers
void clearBuffers()
Clear the a/v buffers.
Definition: MediaParser.cpp:286
IF_VERBOSE_PARSE
#define IF_VERBOSE_PARSE(x)
Definition: log.h:378
log.h
gnash::media::ExtraAudioInfoFlv::size
size_t size
Audio stream header size.
Definition: FLVParser.h:100
Arg_parser::code
int code(const int i) const
Definition: arg_parser.h:113
gnashconfig.h
gnash::media::ExtraAudioInfoFlv::ExtraAudioInfoFlv
ExtraAudioInfoFlv(std::uint8_t *extradata, size_t datasize)
Construct an ExtraAudioInfoFlv.
Definition: FLVParser.h:89
gnash::RcInitFile::getDefaultInstance
static RcInitFile & getDefaultInstance()
Return the default instance of RC file.
Definition: rc.cpp:61
gnash::media::FLVParser::indexingCompleted
bool indexingCompleted() const
Return true of indexing is completed.
Definition: FLVParser.h:139
DSOEXPORT
#define DSOEXPORT
Definition: dsodefs.h:55
gnash::media::MediaParser::OrderedMetaTags
std::vector< MetaTags::mapped_type > OrderedMetaTags
Definition: MediaParser.h:482
gnash::equal
bool equal(string_table &st, string_table::key a, string_table::key b, bool caseless)
Check whether two keys are equivalent.
Definition: string_table.cpp:174
gnash::media::MediaParser::_seekRequest
bool _seekRequest
Definition: MediaParser.h:745
gnash::media::FLVParser
The FLVParser class parses FLV streams.
Definition: FLVParser.h:105
gnash::media::MediaParser::_streamMutex
std::mutex _streamMutex
Definition: MediaParser.h:698
gnash::log_unimpl
void log_unimpl(StringType msg, Args... args)
Definition: log.h:289
gnash::hexify
std::string hexify(const unsigned char *p, size_t length, bool ascii)
Convert a sequence of bytes to hex or ascii format.
Definition: log.cpp:48
gnash::media::ExtraVideoInfoFlv
Extra video info found in some FLV embedded streams.
Definition: FLVParser.h:44
GnashException.h
gnash::MediaException
An exception from MediaHandler subsystem.
Definition: GnashException.h:50
data
SimpleBuffer data
Definition: LocalConnection_as.cpp:151
IOChannel.h
gnash::key::e
@ e
Definition: GnashKey.h:151
arg_parser.h
gnash::media::MediaParser::pushEncodedAudioFrame
void pushEncodedAudioFrame(std::unique_ptr< EncodedAudioFrame > frame)
Push an encoded audio frame to buffer.
Definition: MediaParser.cpp:299
gnash::media::MediaParser::stopParserThread
void stopParserThread()
Stop the parser thread.
Definition: MediaParser.cpp:271
gnash::media::AudioInfo::ExtraInfo
Extra info about an audio stream.
Definition: MediaParser.h:304
gnash::RcInitFile
Definition: rc.h:44
gnash::media::FLVParser::FLVParser
FLVParser(std::unique_ptr< IOChannel > lt)
Create an FLV parser reading input from the given IOChannel.
Definition: FLVParser.cpp:44
gnash::media::FLVParser::~FLVParser
~FLVParser()
Kills the parser...
Definition: FLVParser.cpp:61