Debian Codegraph @

engine/sound/ogg.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name ogg.cpp - ogg support */
11 //
12 // (c) Copyright 2005-2008 by Nehal Mistry
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 // This file contains code for both the ogg file format, and ogg vorbis.
32
33 /*----------------------------------------------------------------------------
34 -- Includes
35 ----------------------------------------------------------------------------*/
36
37 #include "stratagus.h"
38
39 #ifdef USE_VORBIS // {
40
41 #include <stdlib.h>
42 #include <string.h>
43
44 #include <vorbis/codec.h>
45 #include <vorbis/vorbisfile.h>
46 #ifdef USE_THEORA
47 #include <theora/theora.h>
48 #endif
49
50 #include "SDL.h"
51 #include "SDL_endian.h"
52
53 #include "iolib.h"
54 #include "movie.h"
55 #include "sound_server.h"
56
57 /*----------------------------------------------------------------------------
58 -- Declaration
59 ----------------------------------------------------------------------------*/
60
61 class CSampleVorbis : public CSample
62 {
63 public:
64 ~CSampleVorbis();
65 int Read(void *buf, int len);
66
67 OggData Data;
68 };
69
70 class CSampleVorbisStream : public CSample
71 {
72 public:
73 ~CSampleVorbisStream();
74 int Read(void *buf, int len);
75
76 OggData Data;
77 };
78
79 /*----------------------------------------------------------------------------
80 -- Functions
81 ----------------------------------------------------------------------------*/
82
83 int OggGetNextPage(ogg_page *page, ogg_sync_state *sync, CFile *f)
84 {
85 char *buf;
86 int bytes;
87
88 while (ogg_sync_pageout(sync, page) != 1) {
89 // need more bytes
90 buf = ogg_sync_buffer(sync, 4096);
91 bytes = f->read(buf, 4096);
92 if (!bytes || ogg_sync_wrote(sync, bytes)) {
93 return -1;
94 }
95 }
96
97 return 0;
98 }
99
100 int VorbisProcessData(OggData *data, char *buffer)
101 {
102 int num_samples;
103 float **pcm;
104 float *chan;
105 int i, j;
106 int val;
107 ogg_packet packet;
108 int len;
109
110 len = 0;
111 num_samples = 0;
112
113 while (!len) {
114 if (ogg_stream_packetout(&data->astream, &packet) != 1) {
115 if (OggGetNextPage(&data->page, &data->sync, data->File)) {
116 // EOF
117 return -1;
118 }
119
120 ogg_stream_pagein(&data->astream, &data->page);
121 } else {
122 if (vorbis_synthesis(&data->vblock, &packet) == 0) {
123 vorbis_synthesis_blockin(&data->vdsp, &data->vblock);
124 }
125
126 while ((num_samples = vorbis_synthesis_pcmout(&data->vdsp, &pcm)) > 0) {
127 j = 0;
128 for (i = 0; i < data->vinfo.channels; ++i) {
129 chan = pcm[i];
130 for (j = 0; j < num_samples; ++j) {
131 val = static_cast<int>(chan[j] * 32767.f);
132 if (val > 32767) {
133 val = 32767;
134 } else if (val < -32768) {
135 val = -32768;
136 }
137
138 *(Sint16 *)(buffer + len
139 + (j * 2 * data->vinfo.channels) + i * 2) = (Sint16)val;
140 }
141 }
142 len += j * 2 * data->vinfo.channels;
143
144 // we consumed num_samples
145 vorbis_synthesis_read(&data->vdsp, num_samples);
146 }
147 }
148 }
149
150 return len;
151 }
152
153 int OggInit(CFile *f, OggData *data)
154 {
155 ogg_packet packet;
156 int num_vorbis;
157 #ifdef USE_THEORA
158 int num_theora;
159 #endif
160 int stream_start;
161 int ret;
162
163 unsigned magic;
164 f->read(&magic, sizeof(magic));
165 if (SDL_SwapLE32(magic) != 0x5367674F) { // "OggS" in ASCII
166 return -1;
167 }
168 f->seek(0, SEEK_SET);
169
170 ogg_sync_init(&data->sync);
171
172 vorbis_info_init(&data->vinfo);
173 vorbis_comment_init(&data->vcomment);
174
175 #ifdef USE_THEORA
176 theora_info_init(&data->tinfo);
177 theora_comment_init(&data->tcomment);
178 #endif
179
180 #ifdef USE_THEORA
181 num_theora = 0;
182 #endif
183 num_vorbis = 0;
184 stream_start = 0;
185 while (!stream_start) {
186 ogg_stream_state test;
187
188 if (OggGetNextPage(&data->page, &data->sync, f)) {
189 return -1;
190 }
191
192 if (!ogg_page_bos(&data->page)) {
193 if (num_vorbis) {
194 ogg_stream_pagein(&data->astream, &data->page);
195 }
196 #ifdef USE_THEORA
197 if (num_theora) {
198 ogg_stream_pagein(&data->vstream, &data->page);
199 }
200 #endif
201 stream_start = 1;
202 break;
203 }
204
205 ogg_stream_init(&test, ogg_page_serialno(&data->page));
206 ogg_stream_pagein(&test, &data->page);
207
208 // initial codec headers
209 while (ogg_stream_packetout(&test, &packet) == 1) {
210 #ifdef USE_THEORA
211 if (theora_decode_header(&data->tinfo, &data->tcomment, &packet) >= 0) {
212 memcpy(&data->vstream, &test, sizeof(test));
213 ++num_theora;
214 } else
215 #endif
216 if (!vorbis_synthesis_headerin(&data->vinfo, &data->vcomment, &packet)) {
217 memcpy(&data->astream, &test, sizeof(test));
218 ++num_vorbis;
219 } else {
220 ogg_stream_clear(&test);
221 }
222 }
223 }
224
225 data->audio = num_vorbis;
226 #ifdef USE_THEORA
227 data->video = num_theora;
228 #endif
229
230 // remainint codec headers
231 while ((num_vorbis && num_vorbis < 3)
232 #ifdef USE_THEORA
233 || (num_theora && num_theora < 3) ) {
234 // are we in the theora page ?
235 while (num_theora && num_theora < 3 &&
236 (ret = ogg_stream_packetout(&data->vstream, &packet))) {
237 if (ret < 0) {
238 return -1;
239 }
240 if (theora_decode_header(&data->tinfo, &data->tcomment, &packet)) {
241 return -1;
242 }
243 ++num_theora;
244 }
245 #else
246 ) {
247 #endif
248
249 // are we in the vorbis page ?
250 while (num_vorbis && num_vorbis < 3 &&
251 (ret = ogg_stream_packetout(&data->astream, &packet))) {
252 if (ret < 0) {
253 return -1;
254 }
255 if (vorbis_synthesis_headerin(&data->vinfo, &data->vcomment, &packet)) {
256 return -1;
257
258 }
259 ++num_vorbis;
260 }
261
262 if (OggGetNextPage(&data->page, &data->sync, f)) {
263 break;
264 }
265
266 if (num_vorbis) {
267 ogg_stream_pagein(&data->astream, &data->page);
268 }
269 #ifdef USE_THEORA
270 if (num_theora) {
271 ogg_stream_pagein(&data->vstream, &data->page);
272 }
273 #endif
274 }
275
276 if (num_vorbis) {
277 vorbis_synthesis_init(&data->vdsp, &data->vinfo);
278 vorbis_block_init(&data->vdsp, &data->vblock);
279 } else {
280 vorbis_info_clear(&data->vinfo);
281 vorbis_comment_clear(&data->vcomment);
282 }
283
284 #ifdef USE_THEORA
285 if (num_theora) {
286 theora_decode_init(&data->tstate, &data->tinfo);
287 data->tstate.internal_encode = NULL; // needed for a bug in libtheora (fixed in next release)
288 } else {
289 theora_info_clear(&data->tinfo);
290 theora_comment_clear(&data->tcomment);
291 }
292
293 return !(num_vorbis || num_theora);
294 #else
295 return !num_vorbis;
296 #endif
297 }
298
299 void OggFree(OggData *data)
300 {
301 if (data->audio) {
302 ogg_stream_clear(&data->astream);
303 vorbis_block_clear(&data->vblock);
304 vorbis_dsp_clear(&data->vdsp);
305 vorbis_comment_clear(&data->vcomment);
306 vorbis_info_clear(&data->vinfo);
307 }
308 #ifdef USE_THEORA
309 if (data->video) {
310 ogg_stream_clear(&data->vstream);
311 theora_comment_clear(&data->tcomment);
312 theora_info_clear(&data->tinfo);
313 theora_clear(&data->tstate);
314 }
315 #endif
316 ogg_sync_clear(&data->sync);
317 }
318
319
320 int VorbisStreamRead(CSample *sample, OggData *data, void *buf, int len)
321 {
322 int bytes;
323
324 if (sample->Pos > SOUND_BUFFER_SIZE / 2) {
325 memcpy(sample->Buffer, sample->Buffer + sample->Pos, sample->Len);
326 sample->Pos = 0;
327 }
328
329 while (sample->Len < SOUND_BUFFER_SIZE / 4) {
330 bytes = VorbisProcessData(data, (char *)sample->Buffer + sample->Pos + sample->Len);
331 if (bytes > 0) {
332 sample->Len += bytes;
333 } else {
334 break;
335 }
336 }
337
338 if (sample->Len < len) {
339 len = sample->Len;
340 }
341
342 memcpy(buf, sample->Buffer + sample->Pos, len);
343 sample->Pos += len;
344 sample->Len -= len;
345
346 return len;
347 }
348
349 int CSampleVorbisStream::Read(void *buf, int len)
350 {
351 return VorbisStreamRead(this, &this->Data, buf, len);
352 }
353
354 CSampleVorbisStream::~CSampleVorbisStream()
355 {
356 if (this->Data.File) {
357 this->Data.File->close();
358 delete this->Data.File;
359 }
360 OggFree(&this->Data);
361
362 delete[] this->Buffer;
363 }
364
365 int CSampleVorbis::Read(void *buf, int len)
366 {
367 if (len > this->Len) {
368 len = this->Len;
369 }
370
371 memcpy(buf, this->Buffer + this->Pos, len);
372 this->Pos += len;
373 this->Len -= len;
374
375 return len;
376 }
377
378 CSampleVorbis::~CSampleVorbis()
379 {
380 delete[] this->Buffer;
381 }
382
383
384 /**
385 ** Load vorbis.
386 **
387 ** @param name File name.
388 ** @param flags Load flags.
389 **
390 ** @return Returns the loaded sample.
391 */
392 CSample *LoadVorbis(const std::string &name, int flags)
393 {
394 CSample *sample;
395 OggData *data;
396 CFile *f;
397 vorbis_info *info;
398
399 f = new CFile;
400 if (f->open(name.c_str(), CL_OPEN_READ) == -1) {
401 fprintf(stderr, "Can't open file `%s'\n", name.c_str());
402 delete f;
403 return NULL;
404 }
405
406 if (flags & PlayAudioStream) {
407 sample = new CSampleVorbisStream;
408 data = &((CSampleVorbisStream *)sample)->Data;
409 } else {
410 sample = new CSampleVorbis;
411 data = &((CSampleVorbis *)sample)->Data;
412 }
413 memset(data, 0, sizeof(*data));
414
415 if (OggInit(f, data) || !data->audio) {
416 delete sample;
417 f->close();
418 delete f;
419 return NULL;
420 }
421
422 info = &data->vinfo;
423
424 sample->Channels = info->channels;
425 sample->SampleSize = 16;
426 sample->Frequency = info->rate;
427
428 sample->Len = 0;
429 sample->Pos = 0;
430 data->File = f;
431
432 if (flags & PlayAudioStream) {
433 sample->Buffer = new unsigned char[SOUND_BUFFER_SIZE];
434 } else {
435 unsigned char *buf;
436 int pos;
437 int ret;
438 int total;
439 ogg_page pg;
440 ogg_sync_state sync;
441 int i;
442
443 // find total size
444 pos = f->tell();
445 ogg_sync_init(&sync);
446 for (i = 0; ; ++i) {
447 f->seek(-1 * i * 4096, SEEK_END);
448 buf = (unsigned char *)ogg_sync_buffer(&sync, 4096);
449 f->read(buf, 8192);
450 ogg_sync_wrote(&sync, 8192);
451 if (ogg_sync_pageout(&sync, &pg) == 1 && ogg_page_eos(&pg)) {
452 total = (int)(ogg_page_granulepos(&pg) * 2 * sample->Channels);
453 break;
454 }
455 }
456 f->seek(pos, SEEK_SET);
457
458 sample->Buffer = new unsigned char[total];
459 pos = 0;
460
461 while ((ret = VorbisStreamRead(sample, &((CSampleVorbis *)sample)->Data,
462 sample->Buffer + pos, 8192))) {
463 pos += ret;
464 }
465
466 sample->Len = total;
467 sample->Pos = 0;
468
469 f->close();
470 delete f;
471 OggFree(data);
472 }
473
474 return sample;
475 }
476
477 #endif // USE_VORBIS
478
479 //@}