File: ogg.cpp

package info (click to toggle)
freespace2 3.7.0%2Brepack-2
  • links: PTS, VCS
  • area: non-free
  • in suites: jessie, jessie-kfreebsd
  • size: 22,848 kB
  • ctags: 41,897
  • sloc: cpp: 369,931; makefile: 1,060; xml: 129; sh: 112
file content (122 lines) | stat: -rw-r--r-- 2,316 bytes parent folder | download | duplicates (3)
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


#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <Mmsystem.h>
#endif

#define NEED_STRHDL		// for STRHTL struct in audiostr.h

#include "cfile/cfile.h"
#include "sound/ogg/ogg.h"
#include "sound/audiostr.h"


int ogg_inited = 0;
ov_callbacks cfile_callbacks;
ov_callbacks mmio_callbacks;

//Encapsulation funcs to please the almighty ov_callbacks struct
size_t ogg_cfread(void *buf, size_t elsize, size_t elnem, void* cfile)
{
	return cfread(buf, elsize, elnem, (CFILE*)cfile);
}

int ogg_cfseek(void* cfile, ogg_int64_t offset, int where)
{
	return cfseek((CFILE*)cfile, (int) offset, where);
}

int ogg_cfclose(void* cfile)
{
	// we don't close here so that it's safe to do it ourselves
	return 0;
}

long ogg_cftell(void* cfile)
{
	return cftell((CFILE*) cfile);
}


size_t ogg_mmio_read(void *buf, size_t elsize, size_t elnem, void* mmfp)
{
	STRHDL *hdl = (STRHDL*)mmfp;

	return mmioRead(hdl->cfp, (HPSTR) buf, elsize * elnem);
}

int ogg_mmio_seek(void* mmfp, ogg_int64_t offset, int where)
{
	STRHDL *hdl = (STRHDL*)mmfp;

	long rc = 0, cur_offset = 0;

	switch (where) {
		case SEEK_CUR:
		{
			cur_offset = mmioSeek(hdl->cfp, 0, SEEK_CUR);

			if ( (cur_offset + offset) > (hdl->true_offset + hdl->size) )
				return -1;

			rc = mmioSeek(hdl->cfp, cur_offset + (long)offset, SEEK_SET);

			break;
		}

		case SEEK_SET:
		{
			if ( offset > hdl->size )
				return -1;

			rc = mmioSeek(hdl->cfp, hdl->true_offset + (long)offset, SEEK_SET);

			break;
		}

		case SEEK_END:
		{
			rc = mmioSeek(hdl->cfp, hdl->true_offset + hdl->size, SEEK_SET);

			break;
		}
	}

	if ( rc < 0 )
		return -1;

	rc -= hdl->true_offset;

	return (int)rc;
}

int ogg_mmio_close(void* mmfp)
{
	// we don't close here so that it's safe to do it ourselves
	return 0;
}

long ogg_mmio_tell(void* mmfp)
{
	STRHDL *hdl = (STRHDL*)mmfp;

	return (mmioSeek(hdl->cfp, 0, SEEK_CUR) - hdl->true_offset);
}

int OGG_init()
{
	//Setup the cfile_callbacks stucts
	cfile_callbacks.read_func = ogg_cfread;
	cfile_callbacks.seek_func = ogg_cfseek;
	cfile_callbacks.close_func = ogg_cfclose;
	cfile_callbacks.tell_func = ogg_cftell;

	mmio_callbacks.read_func = ogg_mmio_read;
	mmio_callbacks.seek_func = ogg_mmio_seek;
	mmio_callbacks.close_func= ogg_mmio_close;
	mmio_callbacks.tell_func = ogg_mmio_tell;

	return 0;
}