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  // Set the number of prebuffered packets in demuxer
64  // Used only by qdEngine
65  void setPrebufferedPackets(int packets);
66 
67 protected:
68  void readNextPacket();
69  bool useAudioSync() const { return false; }
70 
71 private:
72  class MPEGPSDemuxer {
73  public:
74  MPEGPSDemuxer();
75  ~MPEGPSDemuxer();
76 
77  bool loadStream(Common::SeekableReadStream *stream);
78  void close();
79 
80  Common::SeekableReadStream *getFirstVideoPacket(int32 &startCode, uint32 &pts, uint32 &dts);
81  Common::SeekableReadStream *getNextPacket(uint32 currentTime, int32 &startCode, uint32 &pts, uint32 &dts);
82 
83  void setPrebufferedPackets(int packets) { _prebufferedPackets = packets; }
84 
85  private:
86  class Packet {
87  public:
88  Packet(Common::SeekableReadStream *stream, int32 startCode, uint32 pts, uint32 dts) : _stream(stream), _startCode(startCode), _pts(pts), _dts(dts) {}
89 
91  int32 _startCode;
92  uint32 _pts;
93  uint32 _dts;
94  };
95  bool queueNextPacket();
96  bool fillQueues();
97  int readNextPacketHeader(int32 &startCode, uint32 &pts, uint32 &dts);
98  int findNextStartCode(uint32 &size);
99  uint32 readPTS(int c);
100  void parseProgramStreamMap(int length);
101 
103  Common::Queue<Packet> _videoQueue;
104  Common::Queue<Packet> _audioQueue;
105  // If we come across a non-packetized elementary stream
106  bool _isESStream;
107 
108  uint32 _firstAudioPacketPts = 0xFFFFFFFF;
109  uint32 _firstVideoPacketPts = 0xFFFFFFFF;
110 
111  int _prebufferedPackets = 150;
112  };
113 
114  // Base class for handling MPEG streams
115  class MPEGStream {
116  public:
117  virtual ~MPEGStream() {}
118 
119  enum StreamType {
120  kStreamTypeVideo,
121  kStreamTypeAudio
122  };
123 
124  virtual bool sendPacket(Common::SeekableReadStream *packet, uint32 pts, uint32 dts) = 0;
125  virtual StreamType getStreamType() const = 0;
126  };
127 
128  // An MPEG 1/2 video track
129  class MPEGVideoTrack : public VideoTrack, public MPEGStream {
130  public:
131  MPEGVideoTrack(Common::SeekableReadStream *firstPacket);
132  ~MPEGVideoTrack();
133 
134  bool endOfTrack() const { return _endOfTrack; }
135  uint16 getWidth() const;
136  uint16 getHeight() const;
137  Graphics::PixelFormat getPixelFormat() const;
138  bool setOutputPixelFormat(const Graphics::PixelFormat &format);
139  int getCurFrame() const { return _curFrame; }
140  uint32 getNextFrameStartTime() const { return _nextFrameStartTime.msecs(); }
141  const Graphics::Surface *decodeNextFrame();
142 
143  bool sendPacket(Common::SeekableReadStream *packet, uint32 pts, uint32 dts);
144  StreamType getStreamType() const { return kStreamTypeVideo; }
145 
146  void setEndOfTrack() { _endOfTrack = true; }
147 
148  private:
149  bool _endOfTrack;
150  int _curFrame;
151  uint32 _framePts;
152  Audio::Timestamp _nextFrameStartTime;
153  Graphics::Surface *_surface;
154 
155  uint16 _width;
156  uint16 _height;
157  Graphics::PixelFormat _pixelFormat;
158 
159  void findDimensions(Common::SeekableReadStream *firstPacket);
160 
161 #ifdef USE_MPEG2
162  Image::MPEGDecoder *_mpegDecoder;
163 #endif
164  };
165 
166 #ifdef USE_MAD
167  // An MPEG audio track
168  class MPEGAudioTrack : public AudioTrack, public MPEGStream {
169  public:
170  MPEGAudioTrack(Common::SeekableReadStream &firstPacket, Audio::Mixer::SoundType soundType);
171  ~MPEGAudioTrack();
172 
173  bool sendPacket(Common::SeekableReadStream *packet, uint32 pts, uint32 dts);
174  StreamType getStreamType() const { return kStreamTypeAudio; }
175 
176  protected:
177  Audio::AudioStream *getAudioStream() const;
178 
179  private:
180  Audio::PacketizedAudioStream *_audStream;
181  };
182 #endif
183 
184 #ifdef USE_A52
185  class AC3AudioTrack : public AudioTrack, public MPEGStream {
186  public:
187  AC3AudioTrack(Common::SeekableReadStream &firstPacket, double decibel, Audio::Mixer::SoundType soundType);
188  ~AC3AudioTrack();
189 
190  bool sendPacket(Common::SeekableReadStream *packet, uint32 pts, uint32 dts);
191  StreamType getStreamType() const { return kStreamTypeAudio; }
192 
193  protected:
194  Audio::AudioStream *getAudioStream() const;
195 
196  private:
197  Audio::PacketizedAudioStream *_audStream;
198  };
199 #endif
200 
201  class PS2AudioTrack : public AudioTrack, public MPEGStream {
202  public:
203  PS2AudioTrack(Common::SeekableReadStream *firstPacket, Audio::Mixer::SoundType soundType);
204  ~PS2AudioTrack();
205 
206  bool sendPacket(Common::SeekableReadStream *packet, uint32 pts, uint32 dts);
207  StreamType getStreamType() const { return kStreamTypeAudio; }
208 
209  protected:
210  Audio::AudioStream *getAudioStream() const;
211 
212  private:
213  Audio::PacketizedAudioStream *_audStream;
214 
215  enum {
216  PS2_PCM = 0x01,
217  PS2_ADPCM = 0x10
218  };
219 
220  uint32 _channels;
221  uint32 _soundType;
222  uint32 _interleave;
223  bool _isFirstPacket;
224 
225  byte *_blockBuffer;
226  uint32 _blockPos, _blockUsed;
227 
228  uint32 calculateSampleCount(uint32 packetSize) const;
229  };
230 
231  // The different types of private streams we can detect at the moment
232  enum PrivateStreamType {
233  kPrivateStreamUnknown,
234  kPrivateStreamAC3,
235  kPrivateStreamDTS,
236  kPrivateStreamDVDPCM,
237  kPrivateStreamPS2Audio
238  };
239 
240  PrivateStreamType detectPrivateStreamType(Common::SeekableReadStream *packet);
241 
242  bool addFirstVideoTrack();
243  MPEGStream *getStream(uint32 startCode, Common::SeekableReadStream *packet);
244 
245  MPEGPSDemuxer *_demuxer;
246 
247  // A map from stream types to stream handlers
249  StreamMap _streamMap;
250 
251  double _decibel;
252 };
253 
254 } // End of namespace Video
255 
256 #endif
Definition: surface.h:67
Definition: pixelformat.h:138
Definition: video_decoder.h:723
Definition: timestamp.h:83
Definition: stream.h:745
Definition: audiostream.h:446
SoundType
Definition: mixer.h:73
bool useAudioSync() const
Definition: mpegps_decoder.h:69
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:589