ScummVM API documentation
avi_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_AVI_DECODER_H
23 #define VIDEO_AVI_DECODER_H
24 
25 #include "common/array.h"
26 #include "common/rational.h"
27 #include "common/rect.h"
28 #include "common/str.h"
29 
30 #include "video/video_decoder.h"
31 #include "audio/mixer.h"
32 
33 namespace Audio {
34 class AudioStream;
35 class PacketizedAudioStream;
36 }
37 
38 namespace Common {
39 class SeekableReadStream;
40 }
41 
42 namespace Graphics {
43 struct PixelFormat;
44 }
45 
46 namespace Image {
47 class Codec;
48 }
49 
50 namespace Video {
51 
64 class AVIDecoder : public VideoDecoder {
65 public:
66  AVIDecoder();
67  AVIDecoder(const Common::Rational &frameRateOverride);
68  virtual ~AVIDecoder();
69 
70  bool loadStream(Common::SeekableReadStream *stream);
71  void close();
72  uint16 getWidth() const { return _header.width; }
73  uint16 getHeight() const { return _header.height; }
74 
75  bool rewind();
76  bool isRewindable() const { return true; }
77  bool isSeekable() const;
78 
94  virtual const Graphics::Surface *decodeNextFrame();
95 
99  const Graphics::Surface *decodeNextTransparency();
100 protected:
101  // VideoDecoder API
102  void readNextPacket();
103  bool seekIntern(const Audio::Timestamp &time);
104  bool supportsAudioTrackSwitching() const { return true; }
105  AudioTrack *getAudioTrack(int index);
106 
115  void addTrack(Track *track, bool isExternal = false);
116 
118  uint32 size;
119  uint32 width;
120  uint32 height;
121  uint16 planes;
122  uint16 bitCount;
123  uint32 compression;
124  uint32 sizeImage;
125  uint32 xPelsPerMeter;
126  uint32 yPelsPerMeter;
127  uint32 clrUsed;
128  uint32 clrImportant;
129  };
130 
131  struct WaveFormat {
132  uint16 tag;
133  uint16 channels;
134  uint32 samplesPerSec;
135  uint32 avgBytesPerSec;
136  uint16 blockAlign;
137  };
138 
139  struct PCMWaveFormat : public WaveFormat {
140  uint16 size;
141  };
142 
143  struct WaveFormatEX : public WaveFormat {
144  uint16 bitsPerSample;
145  uint16 size;
146  };
147 
148  struct OldIndex {
149  uint32 id;
150  uint32 flags;
151  uint32 offset;
152  uint32 size;
153  };
154 
155  // Index Flags
156  enum IndexFlags {
157  AVIIF_INDEX = 0x10
158  };
159 
160  struct AVIHeader {
161  uint32 size;
162  uint32 microSecondsPerFrame;
163  uint32 maxBytesPerSecond;
164  uint32 padding;
165  uint32 flags;
166  uint32 totalFrames;
167  uint32 initialFrames;
168  uint32 streams;
169  uint32 bufferSize;
170  uint32 width;
171  uint32 height;
172  };
173 
174  // Flags from the AVIHeader
175  enum AVIFlags {
176  AVIF_HASINDEX = 0x00000010,
177  AVIF_MUSTUSEINDEX = 0x00000020,
178  AVIF_ISINTERLEAVED = 0x00000100,
179  AVIF_TRUSTCKTYPE = 0x00000800,
180  AVIF_WASCAPTUREFILE = 0x00010000,
181  AVIF_WASCOPYRIGHTED = 0x00020000
182  };
183 
185  uint32 size;
186  uint32 streamType;
187  uint32 streamHandler;
188  uint32 flags;
189  uint16 priority;
190  uint16 language;
191  uint32 initialFrames;
192  uint32 scale;
193  uint32 rate;
194  uint32 start;
195  uint32 length;
196  uint32 bufferSize;
197  uint32 quality;
198  uint32 sampleSize;
199  Common::Rect frame;
200  Common::String name;
201  };
202 
204  public:
205  AVIVideoTrack(int frameCount, const AVIStreamHeader &streamHeader, const BitmapInfoHeader &bitmapInfoHeader, byte *initialPalette, Image::CodecAccuracy accuracy);
206  ~AVIVideoTrack();
207 
208  void decodeFrame(Common::SeekableReadStream *stream);
209  void forceTrackEnd();
210 
211  uint16 getWidth() const { return _bmInfo.width; }
212  uint16 getHeight() const { return _bmInfo.height; }
213  uint16 getBitCount() const { return _bmInfo.bitCount; }
214  Graphics::PixelFormat getPixelFormat() const;
215  bool setOutputPixelFormat(const Graphics::PixelFormat &format);
216  void setCodecAccuracy(Image::CodecAccuracy accuracy);
217  int getCurFrame() const { return _curFrame; }
218  int getFrameCount() const { return _frameCount; }
219  Common::String &getName() { return _vidsHeader.name; }
220  const Graphics::Surface *decodeNextFrame() { return _lastFrame; }
221 
222  const byte *getPalette() const;
223  bool hasDirtyPalette() const;
224  void setCurFrame(int frame) { _curFrame = frame; }
225  void loadPaletteFromChunk(Common::SeekableReadStream *chunk);
226  void loadPaletteFromChunkRaw(Common::SeekableReadStream *chunk, int firstEntry, int numEntries);
227  void useInitialPalette();
228  bool canDither() const;
229  void setDither(const byte *palette);
230  bool isValid() const { return _videoCodec != nullptr; }
231 
232  bool isTruemotion1() const;
233  void forceDimensions(uint16 width, uint16 height);
234 
235  bool isRewindable() const { return true; }
236  bool rewind();
237 
246  virtual bool setReverse(bool reverse);
247 
251  virtual bool isReversed() const { return _reversed; }
252 
256  virtual bool endOfTrack() const;
257 
261  Common::Rational getFrameRate() const { return Common::Rational(_vidsHeader.rate, _vidsHeader.scale); }
262 
267  _vidsHeader.rate = r.getNumerator();
268  _vidsHeader.scale = r.getDenominator();
269  }
270  private:
271  AVIStreamHeader _vidsHeader;
272  BitmapInfoHeader _bmInfo;
273  byte _palette[3 * 256];
274  byte *_initialPalette;
275  mutable bool _dirtyPalette;
276  int _frameCount, _curFrame;
277  bool _reversed;
278 
279  Image::Codec *_videoCodec;
280  const Graphics::Surface *_lastFrame;
281  Image::CodecAccuracy _accuracy;
282 
283  Image::Codec *createCodec();
284  };
285 
286  class AVIAudioTrack : public AudioTrack {
287  public:
288  AVIAudioTrack(const AVIStreamHeader &streamHeader, const PCMWaveFormat &waveFormat, Audio::Mixer::SoundType soundType);
289  ~AVIAudioTrack();
290 
291  virtual void createAudioStream();
292  virtual void queueSound(Common::SeekableReadStream *stream);
293  void skipAudio(const Audio::Timestamp &time, const Audio::Timestamp &frameTime);
294  virtual void resetStream();
295  uint32 getCurChunk() const { return _curChunk; }
296  Common::String &getName() { return _audsHeader.name; }
297  void setCurChunk(uint32 chunk) { _curChunk = chunk; }
298 
299  bool isRewindable() const { return true; }
300  bool rewind();
301 
302  protected:
303  Audio::AudioStream *getAudioStream() const { return _audioStream; }
304 
305  AVIStreamHeader _audsHeader;
306  PCMWaveFormat _wvInfo;
307  Audio::AudioStream *_audioStream;
308  Audio::PacketizedAudioStream *_packetStream;
309  uint32 _curChunk;
310  };
311 
312  struct TrackStatus {
313  TrackStatus();
314 
315  Track *track;
316  uint32 index;
317  uint32 chunkSearchOffset;
318  };
319 
320  class IndexEntries : public Common::Array<OldIndex> {
321  public:
322  OldIndex *find(uint index, uint frameNumber);
323  };
324 
325  AVIHeader _header;
326 
327  void readOldIndex(uint32 size);
328  IndexEntries _indexEntries;
329 
330  Common::SeekableReadStream *_fileStream;
331  bool _decodedHeader;
332  bool _foundMovieList;
333  uint32 _movieListStart, _movieListEnd;
334 
335  Common::Rational _frameRateOverride;
336 
337  int _videoTrackCounter, _audioTrackCounter;
338  Track *_lastAddedTrack;
339 
340  void initCommon();
341 
342  bool parseNextChunk();
343  void skipChunk(uint32 size);
344  void handleList(uint32 listSize);
345  void handleStreamHeader(uint32 size);
346  void readStreamName(uint32 size);
347  void readPalette8(uint32 size);
348  uint16 getStreamType(uint32 tag) const { return tag & 0xFFFF; }
349  static byte getStreamIndex(uint32 tag);
350  void checkTruemotion1();
351  uint getVideoTrackOffset(uint trackIndex, uint frameNumber = 0);
352 
353  void handleNextPacket(TrackStatus& status);
354  bool shouldQueueAudio(TrackStatus& status);
355  void seekTransparencyFrame(int frame);
356 
357  Common::Array<TrackStatus> _videoTracks, _audioTracks;
358  TrackStatus _transparencyTrack;
359 
360 public:
361  virtual AVIAudioTrack *createAudioTrack(AVIStreamHeader sHeader, PCMWaveFormat wvInfo);
362 
369  virtual bool seekToFrame(uint frame);
370 };
371 
372 } // End of namespace Video
373 
374 #endif
Definition: avi_decoder.h:131
Definition: str.h:59
Definition: video_decoder.h:493
Definition: surface.h:67
Definition: video_decoder.h:686
Definition: avi_decoder.h:64
Definition: array.h:52
Definition: pixelformat.h:138
In find(In first, In last, const T &v)
Definition: algorithm.h:225
Definition: video_decoder.h:711
Definition: avi_decoder.h:320
Definition: avi_decoder.h:184
Definition: rect.h:144
Common::Rational getFrameRate() const
Definition: avi_decoder.h:261
bool isRewindable() const
Definition: avi_decoder.h:76
Definition: timestamp.h:83
uint16 getHeight() const
Definition: avi_decoder.h:212
Definition: stream.h:745
Definition: audiostream.h:446
const Graphics::Surface * decodeNextFrame()
Definition: avi_decoder.h:220
Definition: rational.h:40
Definition: avi_decoder.h:160
Definition: codec.h:57
Definition: avi_decoder.h:286
Graphics::Surface * scale(const Graphics::Surface &srcImage, int xSize, int ySize)
SoundType
Definition: mixer.h:62
Definition: avi_decoder.h:117
Definition: avi_decoder.h:203
Definition: video_decoder.h:53
Definition: avi_decoder.h:139
Definition: algorithm.h:29
Definition: formatinfo.h:28
Definition: avi_decoder.h:148
Definition: audiostream.h:50
bool supportsAudioTrackSwitching() const
Definition: avi_decoder.h:104
uint16 getWidth() const
Definition: avi_decoder.h:72
bool isRewindable() const
Definition: avi_decoder.h:235
Definition: avi_decoder.h:143
int getCurFrame() const
Definition: avi_decoder.h:217
Definition: avi_decoder.h:312
uint16 getHeight() const
Definition: avi_decoder.h:73
Definition: avi_frames.h:36
Definition: movie_decoder.h:32
void setFrameRate(const Common::Rational &r)
Definition: avi_decoder.h:266
int getFrameCount() const
Definition: avi_decoder.h:218
uint16 getWidth() const
Definition: avi_decoder.h:211
Definition: system.h:38
virtual bool isReversed() const
Definition: avi_decoder.h:251