1 // MediaParserFfmpeg.h: FFMEPG media parsers, 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
20 #ifndef GNASH_MEDIAPARSER_FFMPEG_H
21 #define GNASH_MEDIAPARSER_FFMPEG_H
23 #include <boost/scoped_array.hpp>
25 #include <boost/optional.hpp>
27 #include "MediaParser.h" // for inheritance
28 #include "ffmpegHeaders.h"
31 // Forward declaration
40 /// Extra info found in audio stream by the parser.
42 /// The info will be needed for proper initialization of decoder.
44 class ExtraAudioInfoFfmpeg
: public AudioInfo::ExtraInfo
47 ExtraAudioInfoFfmpeg(boost::uint8_t* nData
, size_t nDataSize
)
57 /// Extra info found in video stream by the parser.
59 /// The info will be needed for proper initialization of decoder.
61 class ExtraVideoInfoFfmpeg
: public VideoInfo::ExtraInfo
64 ExtraVideoInfoFfmpeg(boost::uint8_t* nData
, size_t nDataSize
)
74 /// FFMPEG based MediaParser
75 class MediaParserFfmpeg
: public MediaParser
79 /// Construct a ffmpeg-based media parser for given stream
81 /// Can throw a GnashException if input format couldn't be detected
83 MediaParserFfmpeg(std::auto_ptr
<IOChannel
> stream
);
87 // See dox in MediaParser.h
88 virtual bool seek(boost::uint32_t&);
90 // See dox in MediaParser.h
91 virtual bool parseNextChunk();
93 // See dox in MediaParser.h
94 virtual boost::uint64_t getBytesLoaded() const;
96 virtual boost::optional
<Id3Info
> getId3Info() const;
100 /// Initialize parser, figuring format and
101 /// creating VideoInfo and AudioInfo objects
102 void initializeParser();
104 /// Video frame cursor position
106 /// This is the video frame number that will
107 /// be referenced by nextVideoFrame and nextVideoFrameTimestamp
109 size_t _nextVideoFrame
;
111 /// Audio frame cursor position
113 /// This is the video frame number that will
114 /// be referenced by nextVideoFrame and nextVideoFrameTimestamp
116 size_t _nextAudioFrame
;
118 /// Parse next media frame
120 /// @return false on error or eof, true otherwise
122 bool parseNextFrame();
124 /// Input chunk reader, to be called by ffmpeg parser
125 int readPacket(boost::uint8_t* buf
, int buf_size
);
127 /// ffmpeg callback function
128 static int readPacketWrapper(void* opaque
, boost::uint8_t* buf
, int buf_size
);
130 /// Input stream seeker, to be called by ffmpeg parser
131 boost::int64_t seekMedia(boost::int64_t offset
, int whence
);
133 /// ffmpeg callback function
134 static boost::int64_t seekMediaWrapper(void *opaque
, boost::int64_t offset
, int whence
);
136 /// Read some of the input to figure an AVInputFormat
137 AVInputFormat
* probeStream();
139 AVInputFormat
* _inputFmt
;
141 /// the format (mp3, avi, etc.)
142 AVFormatContext
*_formatCtx
;
144 /// Index of the video stream in input
145 int _videoStreamIndex
;
147 /// Video input stream
148 AVStream
* _videoStream
;
150 /// Index of the audio stream in input
151 int _audioStreamIndex
;
154 AVStream
* _audioStream
;
157 ByteIOContext _byteIOCxt
;
159 /// Size of the ByteIO context buffer
161 /// This seems to be the size of chunks read
162 /// by av_read_frame.
164 static const size_t byteIOBufferSize
= 1024;
166 boost::scoped_array
<unsigned char> _byteIOBuffer
;
168 /// The last parsed position, for getBytesLoaded
169 boost::uint64_t _lastParsedPosition
;
171 /// Return sample size from SampleFormat
173 /// TODO: move somewhere in ffmpeg utils..
175 boost::uint16_t SampleFormatToSampleSize(SampleFormat fmt
);
177 /// Make an EncodedVideoFrame from an AVPacket and push to buffer
179 bool parseVideoFrame(AVPacket
& packet
);
181 /// Make an EncodedAudioFrame from an AVPacket and push to buffer
182 bool parseAudioFrame(AVPacket
& packet
);
184 boost::optional
<Id3Info
> _id3Object
;
188 } // gnash.media.ffmpeg namespace
189 } // gnash.media namespace
192 #endif // __MEDIAPARSER_FFMPEG_H__