summaryrefslogtreecommitdiff
path: root/src/libs/sound/decoders/oggaud.c
blob: 622712067ab7d4827927c4ea321739d36e9a9f8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/* Ogg Vorbis decoder (.ogg adapter)
 */

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "libs/log.h"
#include "port.h"
#include "types.h"
#include "libs/uio.h"
#include "decoder.h"
#ifdef OVCODEC_TREMOR
#	include <tremor/ivorbiscodec.h>
#	include <tremor/ivorbisfile.h>
#else
#	include <vorbis/codec.h>
#	include <vorbis/vorbisfile.h>
#endif  /* OVCODEC_TREMOR */
#include "oggaud.h"


#define THIS_PTR TFB_SoundDecoder* This

static const char* ova_GetName (void);
static bool ova_InitModule (int flags, const TFB_DecoderFormats*);
static void ova_TermModule (void);
static uint32 ova_GetStructSize (void);
static int ova_GetError (THIS_PTR);
static bool ova_Init (THIS_PTR);
static void ova_Term (THIS_PTR);
static bool ova_Open (THIS_PTR, uio_DirHandle *dir, const char *filename);
static void ova_Close (THIS_PTR);
static int ova_Decode (THIS_PTR, void* buf, sint32 bufsize);
static uint32 ova_Seek (THIS_PTR, uint32 pcm_pos);
static uint32 ova_GetFrame (THIS_PTR);

TFB_SoundDecoderFuncs ova_DecoderVtbl = 
{
	ova_GetName,
	ova_InitModule,
	ova_TermModule,
	ova_GetStructSize,
	ova_GetError,
	ova_Init,
	ova_Term,
	ova_Open,
	ova_Close,
	ova_Decode,
	ova_Seek,
	ova_GetFrame,
};

typedef struct tfb_oggsounddecoder
{
	// always the first member
	TFB_SoundDecoder decoder;

	// private
	sint32 last_error;
	OggVorbis_File vf;

} TFB_OggSoundDecoder;

static const TFB_DecoderFormats* ova_formats = NULL;

static size_t
ogg_read (void *ptr, size_t size, size_t nmemb, void *datasource)
{
	return uio_fread (ptr, size, nmemb, (uio_Stream *) datasource);
}

static int
ogg_seek (void *datasource, ogg_int64_t offset, int whence)
{
	long off = (long) offset;
	return uio_fseek ((uio_Stream *) datasource, off, whence);
}

static int
ogg_close (void *datasource)
{
	return uio_fclose ((uio_Stream *) datasource);
}

static long
ogg_tell (void *datasource)
{
	return uio_ftell ((uio_Stream *) datasource);
}

static const ov_callbacks ogg_callbacks = 
{
	ogg_read,
	ogg_seek,
	ogg_close, 
	ogg_tell,
};

static const char*
ova_GetName (void)
{
	return "Ogg Vorbis";
}

static bool
ova_InitModule (int flags, const TFB_DecoderFormats* fmts)
{
	ova_formats = fmts;
	return true;
	
	(void)flags;	// laugh at compiler warning
}

static void
ova_TermModule (void)
{
	// no specific module term
}

static uint32
ova_GetStructSize (void)
{
	return sizeof (TFB_OggSoundDecoder);
}

static int
ova_GetError (THIS_PTR)
{
	TFB_OggSoundDecoder* ova = (TFB_OggSoundDecoder*) This;
	int ret = ova->last_error;
	ova->last_error = 0;
	return ret;
}

static bool
ova_Init (THIS_PTR)
{
	//TFB_OggSoundDecoder* ova = (TFB_OggSoundDecoder*) This;
	This->need_swap = false;
	return true;
}

static void
ova_Term (THIS_PTR)
{
	//TFB_OggSoundDecoder* ova = (TFB_OggSoundDecoder*) This;
	ova_Close (This); // ensure cleanup
}

static bool
ova_Open (THIS_PTR, uio_DirHandle *dir, const char *filename)
{
	TFB_OggSoundDecoder* ova = (TFB_OggSoundDecoder*) This;
	int rc;
	uio_Stream *fp;
	vorbis_info *vinfo;
	
	fp = uio_fopen (dir, filename, "rb");
	if (fp == NULL)
	{
		log_add (log_Warning, "ova_Open(): could not open %s", filename);
		return false;
	}

	rc = ov_open_callbacks (fp, &ova->vf, NULL, 0, ogg_callbacks);
	if (rc != 0)
	{
		log_add (log_Warning, "ova_Open(): "
				"ov_open_callbacks failed for %s, error code %d",
				filename, rc);
		uio_fclose (fp);
		return false;
	}

	vinfo = ov_info (&ova->vf, -1);
	if (!vinfo)
	{
		log_add (log_Warning, "ova_Open(): "
				"failed to retrieve ogg bitstream info for %s",
				filename);
	    ov_clear (&ova->vf);
		return false;
	}
	
	This->frequency = vinfo->rate;
#ifdef OVCODEC_TREMOR
	// With tremor ov_time_total returns an integer, in milliseconds.
	This->length = ((float) ov_time_total (&ova->vf, -1)) / 1000.0f;
#else
	// With libvorbis ov_time_total returns a double, in seconds.
	This->length = (float) ov_time_total (&ova->vf, -1);
#endif  /* OVCODEC_TREMOR */
	
	if (vinfo->channels == 1)
		This->format = ova_formats->mono16;
	else
		This->format = ova_formats->stereo16;

	ova->last_error = 0;

	return true;
}

static void
ova_Close (THIS_PTR)
{
	TFB_OggSoundDecoder* ova = (TFB_OggSoundDecoder*) This;

	ov_clear (&ova->vf);
}

static int
ova_Decode (THIS_PTR, void* buf, sint32 bufsize)
{
	TFB_OggSoundDecoder* ova = (TFB_OggSoundDecoder*) This;
	long rc;
	int bitstream;

#ifdef OVCODEC_TREMOR
	rc = ov_read (&ova->vf, buf, bufsize, &bitstream);
#else
	rc = ov_read (&ova->vf, buf, bufsize, ova_formats->want_big_endian,
			2, 1, &bitstream);
#endif  /* OVCODEC_TREMOR */
	
	if (rc < 0)
		ova->last_error = rc;
	else
		ova->last_error = 0;

	return rc;
}

static uint32
ova_Seek (THIS_PTR, uint32 pcm_pos)
{
	TFB_OggSoundDecoder* ova = (TFB_OggSoundDecoder*) This;
	int ret;

	ret = ov_pcm_seek (&ova->vf, pcm_pos);
	if (ret != 0)
	{
		ova->last_error = ret;
		return (uint32) ov_pcm_tell (&ova->vf);
	}
	else
		return pcm_pos;
}

static uint32
ova_GetFrame (THIS_PTR)
{
	TFB_OggSoundDecoder* ova = (TFB_OggSoundDecoder*) This;
	// this is the closest to a frame there is in ogg vorbis stream
	// doesn't seem to be a func to retrive it
#ifdef OVCODEC_TREMOR
	return ova->vf.os->pageno;
#else
	return ova->vf.os.pageno;
#endif  /* OVCODEC_TREMOR */
}