ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
theora_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_THEORADEC
23 
24 #ifdef USE_THEORADEC
25 
26 #ifndef VIDEO_THEORA_DECODER_H
27 #define VIDEO_THEORA_DECODER_H
28 
29 #include "common/rational.h"
30 #include "video/video_decoder.h"
31 #include "audio/mixer.h"
32 #include "graphics/surface.h"
33 
34 #include <theora/theoradec.h>
35 
36 #ifdef USE_TREMOR
37 #include <tremor/ivorbiscodec.h>
38 #else
39 #include <vorbis/codec.h>
40 #endif
41 
42 namespace Common {
43 class SeekableReadStream;
44 }
45 
46 namespace Audio {
47 class AudioStream;
48 class QueuingAudioStream;
49 }
50 
51 namespace Video {
52 
61 class TheoraDecoder : public VideoDecoder {
62 public:
63  TheoraDecoder();
64  virtual ~TheoraDecoder();
65 
70  bool loadStream(Common::SeekableReadStream *stream);
71  void close();
72 
74  Common::Rational getFrameRate() const;
75 
76 protected:
77  void readNextPacket();
78 
79 private:
80  class TheoraVideoTrack : public VideoTrack {
81  public:
82  TheoraVideoTrack(th_info &theoraInfo, th_setup_info *theoraSetup);
83  ~TheoraVideoTrack();
84 
85  bool endOfTrack() const { return _endOfVideo; }
86  uint16 getWidth() const { return _width; }
87  uint16 getHeight() const { return _height; }
88  Graphics::PixelFormat getPixelFormat() const { return _pixelFormat; }
89  bool setOutputPixelFormat(const Graphics::PixelFormat &format) {
90  if (format.bytesPerPixel != 2 && format.bytesPerPixel != 4)
91  return false;
92  _pixelFormat = format;
93  return true;
94  }
95 
96  int getCurFrame() const { return _curFrame; }
97  const Common::Rational &getFrameRate() const { return _frameRate; }
98  uint32 getNextFrameStartTime() const { return (uint32)(_nextFrameStartTime * 1000); }
99  const Graphics::Surface *decodeNextFrame() { return _displaySurface; }
100 
101  bool decodePacket(ogg_packet &oggPacket);
102  void setEndOfVideo() { _endOfVideo = true; }
103 
104  private:
105  int _curFrame;
106  bool _endOfVideo;
107  Common::Rational _frameRate;
108  double _nextFrameStartTime;
109 
110  Graphics::Surface *_surface;
111  Graphics::Surface *_displaySurface;
112  Graphics::PixelFormat _pixelFormat;
113  int _x;
114  int _y;
115  uint16 _width;
116  uint16 _height;
117  uint16 _surfaceWidth;
118  uint16 _surfaceHeight;
119 
120  th_dec_ctx *_theoraDecode;
121  th_pixel_fmt _theoraPixelFormat;
122 
123  void translateYUVtoRGBA(th_ycbcr_buffer &YUVBuffer);
124  };
125 
126  class VorbisAudioTrack : public AudioTrack {
127  public:
128  VorbisAudioTrack(Audio::Mixer::SoundType soundType, vorbis_info &vorbisInfo);
129  ~VorbisAudioTrack();
130 
131  bool decodeSamples();
132  bool hasAudio() const;
133  bool needsAudio() const;
134  void synthesizePacket(ogg_packet &oggPacket);
135  void setEndOfAudio() { _endOfAudio = true; }
136 
137  protected:
138  Audio::AudioStream *getAudioStream() const;
139 
140  private:
141  // single audio fragment audio buffering
142  int _audioBufferFill;
143  ogg_int16_t *_audioBuffer;
144 
145  Audio::QueuingAudioStream *_audStream;
146 
147  vorbis_block _vorbisBlock;
148  vorbis_dsp_state _vorbisDSP;
149 
150  bool _endOfAudio;
151  };
152 
153  void queuePage(ogg_page *page);
154  int bufferData();
155  bool queueAudio();
156  void ensureAudioBufferSize();
157 
158  Common::SeekableReadStream *_fileStream;
159 
160  ogg_sync_state _oggSync;
161  ogg_page _oggPage;
162  ogg_packet _oggPacket;
163 
164  ogg_stream_state _theoraOut, _vorbisOut;
165  bool _hasVideo, _hasAudio;
166 
167  vorbis_info _vorbisInfo;
168 
169  TheoraVideoTrack *_videoTrack;
170  VorbisAudioTrack *_audioTrack;
171 };
172 
173 } // End of namespace Video
174 
175 #endif
176 
177 #endif
Definition: surface.h:67
Definition: pixelformat.h:138
Definition: stream.h:745
Definition: rational.h:40
SoundType
Definition: mixer.h:62
Definition: algorithm.h:29
Definition: audiostream.h:50
Definition: audiostream.h:370
Definition: avi_frames.h:36
byte bytesPerPixel
Definition: pixelformat.h:139
Definition: system.h:38