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
30namespace Audio {
31class PacketizedAudioStream;
32}
33
34namespace Common {
35class SeekableReadStream;
36}
37
38namespace Graphics {
39struct PixelFormat;
40}
41
42namespace Image {
43class MPEGDecoder;
44}
45
46namespace Video {
47
56public:
57 MPEGPSDecoder(double decibel = 0.0);
58 virtual ~MPEGPSDecoder();
59
60 bool loadStream(Common::SeekableReadStream *stream) override;
61 void close() override;
62
63 // Set the number of prebuffered packets in demuxer
64 // Used only by qdEngine
65 void setPrebufferedPackets(int packets);
66
67protected:
68 void readNextPacket() override;
69 bool useAudioSync() const override { return false; }
70
71private:
72 class MPEGPSDemuxer {
73 public:
74 MPEGPSDemuxer();
75 ~MPEGPSDemuxer();
76
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 override { return _endOfTrack; }
135 uint16 getWidth() const override;
136 uint16 getHeight() const override;
137 Graphics::PixelFormat getPixelFormat() const override;
138 bool setOutputPixelFormat(const Graphics::PixelFormat &format) override;
139 int getCurFrame() const override { return _curFrame; }
140 uint32 getNextFrameStartTime() const override { return _nextFrameStartTime.msecs(); }
141 const Graphics::Surface *decodeNextFrame() override;
142
143 bool sendPacket(Common::SeekableReadStream *packet, uint32 pts, uint32 dts) override;
144 StreamType getStreamType() const override { 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) override;
174 StreamType getStreamType() const override { return kStreamTypeAudio; }
175
176 protected:
177 Audio::AudioStream *getAudioStream() const override;
178
179 private:
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) override;
191 StreamType getStreamType() const override { return kStreamTypeAudio; }
192
193 protected:
194 Audio::AudioStream *getAudioStream() const override;
195
196 private:
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) override;
207 StreamType getStreamType() const override { return kStreamTypeAudio; }
208
209 protected:
210 Audio::AudioStream *getAudioStream() const override;
211
212 private:
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
248 typedef Common::HashMap<int, MPEGStream *> StreamMap;
249 StreamMap _streamMap;
250
251 double _decibel;
252};
253
254} // End of namespace Video
255
256#endif
Definition: audiostream.h:50
SoundType
Definition: mixer.h:73
Definition: audiostream.h:446
Definition: timestamp.h:83
Definition: stream.h:745
Definition: mpeg.h:46
Definition: mpegps_decoder.h:55
void readNextPacket() override
bool useAudioSync() const override
Definition: mpegps_decoder.h:69
bool loadStream(Common::SeekableReadStream *stream) override
void close() override
Definition: video_decoder.h:53
Graphics::PixelFormat getPixelFormat() const
virtual uint16 getHeight() const
bool setOutputPixelFormat(const Graphics::PixelFormat &format)
virtual const Graphics::Surface * decodeNextFrame()
int getCurFrame() const
virtual uint16 getWidth() const
Definition: system.h:39
Definition: algorithm.h:29
Definition: formatinfo.h:28
Definition: pixelformat.h:138
Definition: surface.h:67