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
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 // Disable symbol overrides for FILE and fseek as those are used in the
24 // Vorbis headers.
25 #define FORBIDDEN_SYMBOL_EXCEPTION_FILE
26 #define FORBIDDEN_SYMBOL_EXCEPTION_fseek
27
28 #include "audio/decoders/vorbis.h"
29
30 #ifdef USE_VORBIS
31
32 #include "common/ptr.h"
33 #include "common/stream.h"
34 #include "common/textconsole.h"
35 #include "common/util.h"
36
37 #include "audio/audiostream.h"
38
39 #ifdef USE_TREMOR
40 #ifdef USE_TREMOLO
41 #include <tremolo/ivorbisfile.h>
42 #else
43 #include <tremor/ivorbisfile.h>
44 #endif
45 #else
46 #define OV_EXCLUDE_STATIC_CALLBACKS
47 #include <vorbis/vorbisfile.h>
48 #endif
49
50
51 namespace Audio {
52
53 // These are wrapper functions to allow using a SeekableReadStream object to
54 // provide data to the OggVorbis_File object.
55
read_stream_wrap(void * ptr,size_t size,size_t nmemb,void * datasource)56 static size_t read_stream_wrap(void *ptr, size_t size, size_t nmemb, void *datasource) {
57 Common::SeekableReadStream *stream = (Common::SeekableReadStream *)datasource;
58
59 uint32 result = stream->read(ptr, size * nmemb);
60
61 return result / size;
62 }
63
seek_stream_wrap(void * datasource,ogg_int64_t offset,int whence)64 static int seek_stream_wrap(void *datasource, ogg_int64_t offset, int whence) {
65 Common::SeekableReadStream *stream = (Common::SeekableReadStream *)datasource;
66 stream->seek((int32)offset, whence);
67 return stream->pos();
68 }
69
close_stream_wrap(void * datasource)70 static int close_stream_wrap(void *datasource) {
71 // Do nothing -- we leave it up to the VorbisStream to free memory as appropriate.
72 return 0;
73 }
74
tell_stream_wrap(void * datasource)75 static long tell_stream_wrap(void *datasource) {
76 Common::SeekableReadStream *stream = (Common::SeekableReadStream *)datasource;
77 return stream->pos();
78 }
79
80 static ov_callbacks g_stream_wrap = {
81 read_stream_wrap, seek_stream_wrap, close_stream_wrap, tell_stream_wrap
82 };
83
84
85
86 #pragma mark -
87 #pragma mark --- Ogg Vorbis stream ---
88 #pragma mark -
89
90
91 class VorbisStream : public SeekableAudioStream {
92 protected:
93 Common::DisposablePtr<Common::SeekableReadStream> _inStream;
94
95 bool _isStereo;
96 int _rate;
97
98 Timestamp _length;
99
100 OggVorbis_File _ovFile;
101
102 int16 _buffer[4096];
103 const int16 *_bufferEnd;
104 const int16 *_pos;
105
106 public:
107 // startTime / duration are in milliseconds
108 VorbisStream(Common::SeekableReadStream *inStream, DisposeAfterUse::Flag dispose);
109 ~VorbisStream();
110
111 int readBuffer(int16 *buffer, const int numSamples);
112
endOfData() const113 bool endOfData() const { return _pos >= _bufferEnd; }
isStereo() const114 bool isStereo() const { return _isStereo; }
getRate() const115 int getRate() const { return _rate; }
116
117 bool seek(const Timestamp &where);
getLength() const118 Timestamp getLength() const { return _length; }
119 protected:
120 bool refill();
121 };
122
VorbisStream(Common::SeekableReadStream * inStream,DisposeAfterUse::Flag dispose)123 VorbisStream::VorbisStream(Common::SeekableReadStream *inStream, DisposeAfterUse::Flag dispose) :
124 _inStream(inStream, dispose),
125 _length(0, 1000),
126 _bufferEnd(ARRAYEND(_buffer)) {
127
128 int res = ov_open_callbacks(inStream, &_ovFile, NULL, 0, g_stream_wrap);
129 if (res < 0) {
130 warning("Could not create Vorbis stream (%d)", res);
131 _pos = _bufferEnd;
132 return;
133 }
134
135 // Read in initial data
136 if (!refill())
137 return;
138
139 // Setup some header information
140 _isStereo = ov_info(&_ovFile, -1)->channels >= 2;
141 _rate = ov_info(&_ovFile, -1)->rate;
142
143 #ifdef USE_TREMOR
144 _length = Timestamp(ov_time_total(&_ovFile, -1), getRate());
145 #else
146 _length = Timestamp(uint32(ov_time_total(&_ovFile, -1) * 1000.0), getRate());
147 #endif
148 }
149
~VorbisStream()150 VorbisStream::~VorbisStream() {
151 ov_clear(&_ovFile);
152 }
153
readBuffer(int16 * buffer,const int numSamples)154 int VorbisStream::readBuffer(int16 *buffer, const int numSamples) {
155 int samples = 0;
156 while (samples < numSamples && _pos < _bufferEnd) {
157 const int len = MIN(numSamples - samples, (int)(_bufferEnd - _pos));
158 memcpy(buffer, _pos, len * 2);
159 buffer += len;
160 _pos += len;
161 samples += len;
162 if (_pos >= _bufferEnd) {
163 if (!refill())
164 break;
165 }
166 }
167 return samples;
168 }
169
seek(const Timestamp & where)170 bool VorbisStream::seek(const Timestamp &where) {
171 // Vorbisfile uses the sample pair number, thus we always use "false" for the isStereo parameter
172 // of the convertTimeToStreamPos helper.
173 int res = ov_pcm_seek(&_ovFile, convertTimeToStreamPos(where, getRate(), false).totalNumberOfFrames());
174 if (res) {
175 warning("Error seeking in Vorbis stream (%d)", res);
176 _pos = _bufferEnd;
177 return false;
178 }
179
180 return refill();
181 }
182
refill()183 bool VorbisStream::refill() {
184 // Read the samples
185 uint len_left = sizeof(_buffer);
186 char *read_pos = (char *)_buffer;
187
188 while (len_left > 0) {
189 long result;
190
191 #ifdef USE_TREMOR
192 // Tremor ov_read() always returns data as signed 16 bit interleaved PCM
193 // in host byte order. As such, it does not take arguments to request
194 // specific signedness, byte order or bit depth as in Vorbisfile.
195 result = ov_read(&_ovFile, read_pos, len_left,
196 NULL);
197 #else
198 #ifdef SCUMM_BIG_ENDIAN
199 result = ov_read(&_ovFile, read_pos, len_left,
200 1,
201 2, // 16 bit
202 1, // signed
203 NULL);
204 #else
205 result = ov_read(&_ovFile, read_pos, len_left,
206 0,
207 2, // 16 bit
208 1, // signed
209 NULL);
210 #endif
211 #endif
212 if (result == OV_HOLE) {
213 // Possibly recoverable, just warn about it
214 warning("Corrupted data in Vorbis file");
215 } else if (result == 0) {
216 //warning("End of file while reading from Vorbis file");
217 //_pos = _bufferEnd;
218 //return false;
219 break;
220 } else if (result < 0) {
221 warning("Error reading from Vorbis stream (%d)", int(result));
222 _pos = _bufferEnd;
223 // Don't delete it yet, that causes problems in
224 // the CD player emulation code.
225 return false;
226 } else {
227 len_left -= result;
228 read_pos += result;
229 }
230 }
231
232 _pos = _buffer;
233 _bufferEnd = (int16 *)read_pos;
234
235 return true;
236 }
237
238
239 #pragma mark -
240 #pragma mark --- Ogg Vorbis factory functions ---
241 #pragma mark -
242
makeVorbisStream(Common::SeekableReadStream * stream,DisposeAfterUse::Flag disposeAfterUse)243 SeekableAudioStream *makeVorbisStream(
244 Common::SeekableReadStream *stream,
245 DisposeAfterUse::Flag disposeAfterUse) {
246 SeekableAudioStream *s = new VorbisStream(stream, disposeAfterUse);
247 if (s && s->endOfData()) {
248 delete s;
249 return 0;
250 } else {
251 return s;
252 }
253 }
254
255 } // End of namespace Audio
256
257 #endif // #ifdef USE_VORBIS
258