ScummVM API documentation
mpegps_decoder.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef VIDEO_MPEGPS_DECODER_H
23 #define VIDEO_MPEGPS_DECODER_H
24 
25 #include "common/hashmap.h"
26 #include "common/queue.h"
27 #include "graphics/surface.h"
28 #include "video/video_decoder.h"
29 
30 namespace Audio {
31 class PacketizedAudioStream;
32 }
33 
34 namespace Common {
35 class SeekableReadStream;
36 }
37 
38 namespace Graphics {
39 struct PixelFormat;
40 }
41 
42 namespace Image {
43 class MPEGDecoder;
44 }
45 
46 namespace Video {
47 
55 class MPEGPSDecoder : public VideoDecoder {
56 public:
57  MPEGPSDecoder(double decibel = 0.0);
58  virtual ~MPEGPSDecoder();
59 
60  bool loadStream(Common::SeekableReadStream *stream);
61  void close();
62 
63 protected:
64  void readNextPacket();
65  bool useAudioSync() const { return false; }
66 
67 private:
68  class MPEGPSDemuxer {
69  public:
70  MPEGPSDemuxer();
71  ~MPEGPSDemuxer();
72 
73  bool loadStream(Common::SeekableReadStream *stream);
74  void close();
75 
76  Common::SeekableReadStream *getFirstVideoPacket(int32 &startCode, uint32 &pts, uint32 &dts);
77  Common::SeekableReadStream *getNextPacket(uint32 currentTime, int32 &startCode, uint32 &pts, uint32 &dts);
78 
79  private:
80  class Packet {
81  public:
82  Packet(Common::SeekableReadStream *stream, int32 startCode, uint32 pts, uint32 dts) : _stream(stream), _startCode(startCode), _pts(pts), _dts(dts) {}
83 
85  int32 _startCode;
86  uint32 _pts;
87  uint32 _dts;
88  };
89  bool queueNextPacket();
90  bool fillQueues();
91  int readNextPacketHeader(int32 &startCode, uint32 &pts, uint32 &dts);
92  int findNextStartCode(uint32 &size);
93  uint32 readPTS(int c);
94  void parseProgramStreamMap(int length);
95 
97  Common::Queue<Packet> _videoQueue;
98  Common::Queue<Packet> _audioQueue;
99  };
100 
101  // Base class for handling MPEG streams
102  class MPEGStream {
103  public:
104  virtual ~MPEGStream() {}
105 
106  enum StreamType {
107  kStreamTypeVideo,
108  kStreamTypeAudio
109  };
110 
111  virtual bool sendPacket(Common::SeekableReadStream *packet, uint32 pts, uint32 dts) = 0;
112  virtual StreamType getStreamType() const = 0;
113  };
114 
115  // An MPEG 1/2 video track
116  class MPEGVideoTrack : public VideoTrack, public MPEGStream {
117  public:
118  MPEGVideoTrack(Common::SeekableReadStream *firstPacket);
119  ~MPEGVideoTrack();
120 
121  bool endOfTrack() const { return _endOfTrack; }
122  uint16 getWidth() const;
123  uint16 getHeight() const;
124  Graphics::PixelFormat getPixelFormat() const;
125  bool setOutputPixelFormat(const Graphics::PixelFormat &format);
126  int getCurFrame() const { return _curFrame; }
127  uint32 getNextFrameStartTime() const { return _nextFrameStartTime.msecs(); }
128  const Graphics::Surface *decodeNextFrame();
129 
130  bool sendPacket(Common::SeekableReadStream *packet, uint32 pts, uint32 dts);
131  StreamType getStreamType() const { return kStreamTypeVideo; }
132 
133  void setEndOfTrack() { _endOfTrack = true; }
134 
135  private:
136  bool _endOfTrack;
137  int _curFrame;
138  uint32 _framePts;
139  Audio::Timestamp _nextFrameStartTime;
140  Graphics::Surface *_surface;
141 
142  uint16 _width;
143  uint16 _height;
144  Graphics::PixelFormat _pixelFormat;
145 
146  void findDimensions(Common::SeekableReadStream *firstPacket);
147 
148 #ifdef USE_MPEG2
149  Image::MPEGDecoder *_mpegDecoder;
150 #endif
151  };
152 
153 #ifdef USE_MAD
154  // An MPEG audio track
155  class MPEGAudioTrack : public AudioTrack, public MPEGStream {
156  public:
157  MPEGAudioTrack(Common::SeekableReadStream &firstPacket, Audio::Mixer::SoundType soundType);
158  ~MPEGAudioTrack();
159 
160  bool sendPacket(Common::SeekableReadStream *packet, uint32 pts, uint32 dts);
161  StreamType getStreamType() const { return kStreamTypeAudio; }
162 
163  protected:
164  Audio::AudioStream *getAudioStream() const;
165 
166  private:
167  Audio::PacketizedAudioStream *_audStream;
168  };
169 #endif
170 
171 #ifdef USE_A52
172  class AC3AudioTrack : public AudioTrack, public MPEGStream {
173  public:
174  AC3AudioTrack(Common::SeekableReadStream &firstPacket, double decibel, Audio::Mixer::SoundType soundType);
175  ~AC3AudioTrack();
176 
177  bool sendPacket(Common::SeekableReadStream *packet, uint32 pts, uint32 dts);
178  StreamType getStreamType() const { return kStreamTypeAudio; }
179 
180  protected:
181  Audio::AudioStream *getAudioStream() const;
182 
183  private:
184  Audio::PacketizedAudioStream *_audStream;
185  };
186 #endif
187 
188  class PS2AudioTrack : public AudioTrack, public MPEGStream {
189  public:
190  PS2AudioTrack(Common::SeekableReadStream *firstPacket, Audio::Mixer::SoundType soundType);
191  ~PS2AudioTrack();
192 
193  bool sendPacket(Common::SeekableReadStream *packet, uint32 pts, uint32 dts);
194  StreamType getStreamType() const { return kStreamTypeAudio; }
195 
196  protected:
197  Audio::AudioStream *getAudioStream() const;
198 
199  private:
200  Audio::PacketizedAudioStream *_audStream;
201 
202  enum {
203  PS2_PCM = 0x01,
204  PS2_ADPCM = 0x10
205  };
206 
207  uint32 _channels;
208  uint32 _soundType;
209  uint32 _interleave;
210  bool _isFirstPacket;
211 
212  byte *_blockBuffer;
213  uint32 _blockPos, _blockUsed;
214 
215  uint32 calculateSampleCount(uint32 packetSize) const;
216  };
217 
218  // The different types of private streams we can detect at the moment
219  enum PrivateStreamType {
220  kPrivateStreamUnknown,
221  kPrivateStreamAC3,
222  kPrivateStreamDTS,
223  kPrivateStreamDVDPCM,
224  kPrivateStreamPS2Audio
225  };
226 
227  PrivateStreamType detectPrivateStreamType(Common::SeekableReadStream *packet);
228 
229  bool addFirstVideoTrack();
230  MPEGStream *getStream(uint32 startCode, Common::SeekableReadStream *packet);
231 
232  MPEGPSDemuxer *_demuxer;
233 
234  // A map from stream types to stream handlers
236  StreamMap _streamMap;
237 
238  double _decibel;
239 };
240 
241 } // End of namespace Video
242 
243 #endif
Definition: surface.h:67
Definition: pixelformat.h:138
Definition: video_decoder.h:711
Definition: timestamp.h:83
Definition: stream.h:745
Definition: audiostream.h:446
SoundType
Definition: mixer.h:62
bool useAudioSync() const
Definition: mpegps_decoder.h:65
Definition: video_decoder.h:53
Definition: algorithm.h:29
Definition: formatinfo.h:28
Definition: audiostream.h:50
Definition: avi_frames.h:36
Definition: mpegps_decoder.h:55
Definition: movie_decoder.h:32
Definition: system.h:38
Definition: video_decoder.h:577