2019-11-24 20:45:15 -08:00
|
|
|
/*
|
|
|
|
* Ogg/Vorbis streaming music support, loosely based on several open source
|
|
|
|
* Quake engine based projects with many modifications.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010-2012 O.Sezer <sezero@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "quakedef.h"
|
|
|
|
|
|
|
|
#if defined(USE_CODEC_VORBIS)
|
|
|
|
#include "snd_codec.h"
|
|
|
|
#include "snd_codeci.h"
|
|
|
|
#include "snd_vorbis.h"
|
|
|
|
|
|
|
|
#define OV_EXCLUDE_STATIC_CALLBACKS
|
|
|
|
#if defined(VORBIS_USE_TREMOR)
|
|
|
|
/* for Tremor / Vorbisfile api differences,
|
|
|
|
* see doc/diff.html in the Tremor package. */
|
|
|
|
#include <tremor/ivorbisfile.h>
|
|
|
|
#else
|
|
|
|
#include <vorbis/vorbisfile.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Vorbis codec can return the samples in a number of different
|
2019-11-25 14:15:33 -08:00
|
|
|
* formats, we use the standard int16_t format. */
|
2019-11-24 20:45:15 -08:00
|
|
|
#define VORBIS_SAMPLEBITS 16
|
|
|
|
#define VORBIS_SAMPLEWIDTH 2
|
|
|
|
#define VORBIS_SIGNED_DATA 1
|
|
|
|
|
|
|
|
/* CALLBACK FUNCTIONS: */
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
static int32_t ovc_fclose(void *f)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 13:33:54 -08:00
|
|
|
(void)f;
|
2019-11-25 17:40:18 -08:00
|
|
|
return 0; /* we fclose() elsewhere. */
|
2019-11-24 20:45:15 -08:00
|
|
|
}
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
static int32_t ovc_fseek(void *f, ogg_int64_t off, int32_t whence)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
if(f == NULL) return (-1);
|
2019-11-24 20:45:15 -08:00
|
|
|
return FS_fseek((fshandle_t *)f, (long) off, whence);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ov_callbacks ovc_qfs =
|
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
(size_t (*)(void *, size_t, size_t, void *)) FS_fread,
|
|
|
|
(int32_t (*)(void *, ogg_int64_t, int32_t)) ovc_fseek,
|
|
|
|
(int32_t (*)(void *)) ovc_fclose,
|
|
|
|
(long (*)(void *)) FS_ftell
|
2019-11-24 20:45:15 -08:00
|
|
|
};
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
static bool S_VORBIS_CodecInitialize(void)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
static void S_VORBIS_CodecShutdown(void)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
static bool S_VORBIS_CodecOpenStream(snd_stream_t *stream)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
|
|
|
OggVorbis_File *ovFile;
|
|
|
|
vorbis_info *ovf_info;
|
|
|
|
long numstreams;
|
2019-11-25 16:49:58 -08:00
|
|
|
int32_t res;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
ovFile = (OggVorbis_File *) Z_Malloc(sizeof(OggVorbis_File));
|
|
|
|
stream->priv = ovFile;
|
|
|
|
res = ov_open_callbacks(&stream->fh, ovFile, NULL, 0, ovc_qfs);
|
2019-11-25 17:40:18 -08:00
|
|
|
if(res != 0)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 16:56:15 -08:00
|
|
|
Con_Printf("%s is not a valid Ogg Vorbis file (error %" PRIi32 ").\n",
|
2019-11-25 17:40:18 -08:00
|
|
|
stream->name, res);
|
2019-11-24 20:45:15 -08:00
|
|
|
goto _fail;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
if(!ov_seekable(ovFile))
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
|
|
|
Con_Printf("Stream %s not seekable.\n", stream->name);
|
|
|
|
goto _fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ovf_info = ov_info(ovFile, 0);
|
2019-11-25 17:40:18 -08:00
|
|
|
if(!ovf_info)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
|
|
|
Con_Printf("Unable to get stream info for %s.\n", stream->name);
|
|
|
|
goto _fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: handle section changes */
|
|
|
|
numstreams = ov_streams(ovFile);
|
2019-11-25 17:40:18 -08:00
|
|
|
if(numstreams != 1)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 16:56:15 -08:00
|
|
|
Con_Printf("More than one (%" PRIi64 ") stream in %s.\n",
|
2019-11-25 17:40:18 -08:00
|
|
|
numstreams, stream->name);
|
2019-11-24 20:45:15 -08:00
|
|
|
goto _fail;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
if(ovf_info->channels != 1 && ovf_info->channels != 2)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 16:56:15 -08:00
|
|
|
Con_Printf("Unsupported number of channels %" PRIi32 " in %s\n",
|
2019-11-25 17:40:18 -08:00
|
|
|
ovf_info->channels, stream->name);
|
2019-11-24 20:45:15 -08:00
|
|
|
goto _fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->info.rate = ovf_info->rate;
|
|
|
|
stream->info.channels = ovf_info->channels;
|
|
|
|
stream->info.bits = VORBIS_SAMPLEBITS;
|
|
|
|
stream->info.width = VORBIS_SAMPLEWIDTH;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
_fail:
|
2019-11-25 17:40:18 -08:00
|
|
|
if(res == 0)
|
2019-11-24 20:45:15 -08:00
|
|
|
ov_clear(ovFile);
|
|
|
|
Z_Free(ovFile);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
static int32_t S_VORBIS_CodecReadStream(snd_stream_t *stream, int32_t bytes, void *buffer)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
int32_t section; /* FIXME: handle section changes */
|
|
|
|
int32_t cnt, res, rem;
|
|
|
|
char * ptr;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
cnt = 0;
|
|
|
|
rem = bytes;
|
2019-11-24 20:45:15 -08:00
|
|
|
ptr = (char *) buffer;
|
2019-11-25 17:40:18 -08:00
|
|
|
while(1)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
/* # ov_read() from libvorbisfile returns the decoded PCM audio
|
|
|
|
* in requested endianness, signedness and word size.
|
|
|
|
* # ov_read() from Tremor (libvorbisidec) returns decoded audio
|
|
|
|
* always in host-endian, signed 16 bit PCM format.
|
|
|
|
* # For both of the libraries, if the audio is multichannel,
|
|
|
|
* the channels are interleaved in the output buffer.
|
|
|
|
*/
|
|
|
|
res = ov_read((OggVorbis_File *)stream->priv, ptr, rem,
|
2019-11-24 20:45:15 -08:00
|
|
|
#ifndef VORBIS_USE_TREMOR
|
2019-11-25 17:40:18 -08:00
|
|
|
host_bigendian,
|
|
|
|
VORBIS_SAMPLEWIDTH,
|
|
|
|
VORBIS_SIGNED_DATA,
|
2019-11-24 20:45:15 -08:00
|
|
|
#endif
|
2019-11-25 17:40:18 -08:00
|
|
|
& section);
|
|
|
|
if(res <= 0)
|
2019-11-24 20:45:15 -08:00
|
|
|
break;
|
|
|
|
rem -= res;
|
|
|
|
cnt += res;
|
2019-11-25 17:40:18 -08:00
|
|
|
if(rem <= 0)
|
2019-11-24 20:45:15 -08:00
|
|
|
break;
|
|
|
|
ptr += res;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
if(res < 0)
|
2019-11-24 20:45:15 -08:00
|
|
|
return res;
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
static void S_VORBIS_CodecCloseStream(snd_stream_t *stream)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
|
|
|
ov_clear((OggVorbis_File *)stream->priv);
|
|
|
|
Z_Free(stream->priv);
|
|
|
|
S_CodecUtilClose(&stream);
|
|
|
|
}
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
static int32_t S_VORBIS_CodecRewindStream(snd_stream_t *stream)
|
2019-11-24 20:45:15 -08:00
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
/* for libvorbisfile, the ov_time_seek() position argument
|
|
|
|
* is seconds as doubles, whereas for Tremor libvorbisidec
|
|
|
|
* it is milliseconds as 64 bit integers.
|
|
|
|
*/
|
|
|
|
return ov_time_seek((OggVorbis_File *)stream->priv, 0);
|
2019-11-24 20:45:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
snd_codec_t vorbis_codec =
|
|
|
|
{
|
|
|
|
CODECTYPE_VORBIS,
|
2019-11-25 17:40:18 -08:00
|
|
|
true, /* always available. */
|
2019-11-24 20:45:15 -08:00
|
|
|
"ogg",
|
|
|
|
S_VORBIS_CodecInitialize,
|
|
|
|
S_VORBIS_CodecShutdown,
|
|
|
|
S_VORBIS_CodecOpenStream,
|
|
|
|
S_VORBIS_CodecReadStream,
|
|
|
|
S_VORBIS_CodecRewindStream,
|
|
|
|
S_VORBIS_CodecCloseStream,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
#endif /* USE_CODEC_VORBIS */
|
2019-11-24 20:45:15 -08:00
|
|
|
|