ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
mkv_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 #include "common/scummsys.h" // for USE_VPX
23 
24 #ifdef USE_VPX
25 
26 #ifndef VIDEO_MKV_DECODER_H
27 #define VIDEO_MKV_DECODER_H
28 
29 #include "common/queue.h"
30 #include "video/video_decoder.h"
31 #include "audio/mixer.h"
32 #include "graphics/surface.h"
33 
34 #ifdef USE_TREMOR
35 #include <tremor/ivorbiscodec.h>
36 #else
37 #include <vorbis/codec.h>
38 #endif
39 
40 #include <vpx/vpx_decoder.h>
41 #include <vpx/vp8dx.h>
42 
43 
44 namespace Common {
45 class SeekableReadStream;
46 }
47 
48 namespace Audio {
49 class AudioStream;
50 class QueuingAudioStream;
51 }
52 
53 namespace mkvparser {
54 class MkvReader;
55 class Cluster;
56 class Track;
57 class Tracks;
58 class Block;
59 class BlockEntry;
60 class Segment;
61 }
62 
63 namespace Video {
64 
65 class MkvReader;
66 
73 class MKVDecoder : public VideoDecoder {
74 public:
75  MKVDecoder();
76  virtual ~MKVDecoder();
77 
82  bool loadStream(Common::SeekableReadStream *stream);
83  void close();
84 
85 protected:
86  void readNextPacket();
87 
88 private:
89  class VPXVideoTrack : public VideoTrack {
90  public:
91  VPXVideoTrack(const mkvparser::Track *const pTrack);
92  ~VPXVideoTrack();
93 
94  bool endOfTrack() const;
95  uint16 getWidth() const { return _width; }
96  uint16 getHeight() const { return _height; }
97  Graphics::PixelFormat getPixelFormat() const { return _pixelFormat; }
98  bool setOutputPixelFormat(const Graphics::PixelFormat &format) {
99  if (format.bytesPerPixel != 2 && format.bytesPerPixel != 4)
100  return false;
101  _pixelFormat = format;
102  return true;
103  }
104 
105  int getCurFrame() const { return _curFrame; }
106  uint32 getNextFrameStartTime() const { return (uint32)(_nextFrameStartTime * 1000); }
107  const Graphics::Surface *decodeNextFrame();
108  bool decodeFrame(byte *frame, long size);
109  void setEndOfVideo() { _endOfVideo = true; }
110 
111  private:
112  int _curFrame;
113  bool _endOfVideo;
114  double _nextFrameStartTime;
115 
116  uint16 _width;
117  uint16 _height;
118  Graphics::PixelFormat _pixelFormat;
119 
120  Graphics::Surface _surface;
121  Common::Queue<Graphics::Surface> _displayQueue;
122 
123  vpx_codec_ctx_t *_codec = nullptr;
124  };
125 
126  class VorbisAudioTrack : public AudioTrack {
127  public:
128  VorbisAudioTrack(const mkvparser::Track *const pTrack);
129  ~VorbisAudioTrack();
130 
131  bool decodeSamples(byte *frame, long size);
132  bool hasAudio() const;
133  bool needsAudio() const;
134  bool synthesizePacket(byte *frame, long size);
135  void setEndOfAudio() { _endOfAudio = true; }
136 
137  protected:
138  Audio::AudioStream *getAudioStream() const;
139 
140  private:
141  Audio::QueuingAudioStream *_audStream;
142 
143  vorbis_block _vorbisBlock;
144  vorbis_dsp_state _vorbisDSP;
145 
146  vorbis_info _vorbisInfo;
147  bool _endOfAudio;
148 
149  ogg_packet oggPacket;
150  };
151 
152  bool queueAudio(long size);
153 
154  Common::SeekableReadStream *_fileStream;
155 
156  bool _hasVideo, _hasAudio;
157 
158  VPXVideoTrack *_videoTrack = nullptr;
159  VorbisAudioTrack *_audioTrack = nullptr;
160 
161  mkvparser::MkvReader *_reader = nullptr;
162 
163  const mkvparser::Cluster *_cluster = nullptr;
164  const mkvparser::Tracks *_pTracks = nullptr;
165  const mkvparser::BlockEntry *_pBlockEntry = nullptr;
166  mkvparser::Segment *_pSegment = nullptr;
167 
168  byte *_frame = nullptr;
169  int _frameCounter = 0;
170 
171  int _vTrack = -1;
172  int _aTrack = -1;
173 
174  const mkvparser::Block *_pBlock;
175  long long _trackNum;
176  int _frameCount;
177 };
178 
179 } // End of namespace Video
180 
181 #endif
182 
183 #endif
Definition: surface.h:67
Definition: mkvparser.h:299
Definition: pixelformat.h:138
Definition: mkvparser.h:71
Definition: stream.h:745
Definition: mkvparser.h:13
Definition: mkvparser.h:119
Definition: queue.h:42
Definition: mkvparser.h:1050
Definition: algorithm.h:29
Definition: mkvparser.h:566
Definition: audiostream.h:50
Definition: mkvparser.h:970
Definition: audiostream.h:370
Definition: avi_frames.h:36
byte bytesPerPixel
Definition: pixelformat.h:139
Definition: system.h:38