summaryrefslogtreecommitdiff
path: root/source/external/Wuff/wuff.c
blob: f359733305be96e6a0741c365471a63878781bd3 (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
#include <string.h>

#include "wuff_config.h"
#include "wuff.h"
#include "wuff_internal.h"


wuff_sint32 wuff_open(struct wuff_handle ** handle_pointer, struct wuff_callback * callback, void * userdata)
{
	struct wuff_handle * handle;
	wuff_sint32 wuff_status;

	if (handle_pointer == NULL || callback == NULL)
		return WUFF_INVALID_PARAM;

	handle = wuff_alloc(sizeof(struct wuff_handle));
	if (handle == NULL)
		return WUFF_MEMALLOC_ERROR;

	memset(handle, 0, sizeof(struct wuff_handle));
	handle->buffer.data = NULL;
	handle->callback = callback;
	handle->userdata = userdata;

	wuff_status = wuff_setup(handle);
	if (wuff_status < 0)
	{
		wuff_cleanup(handle);
		return wuff_status;
	}

	*handle_pointer = handle;

	return WUFF_SUCCESS;
}

wuff_sint32 wuff_close(struct wuff_handle * handle)
{
	wuff_sint32 wuff_status;

	if (handle == NULL)
		return WUFF_INVALID_PARAM;

	wuff_status = wuff_cleanup(handle);
	WUFF_STATUS_BAIL()

	return WUFF_SUCCESS;
}

wuff_sint32 wuff_seek(struct wuff_handle * handle, wuff_uint64 offset)
{
	wuff_sint32 wuff_status;
	wuff_uint64 seek_offset;

	if (handle == NULL)
		return WUFF_INVALID_PARAM;

	/* Clamp offset to stream length. */
	offset = offset <= handle->stream.length ? offset : handle->stream.length;
	seek_offset = offset * handle->stream.header.block_size;
	wuff_status = handle->callback->seek(handle->userdata, handle->stream.data.offset + seek_offset);
	WUFF_STATUS_BAIL()

	handle->stream.position = offset;
	handle->output.block_offset = 0;

	/* A new position requires an empty buffer. */
	wuff_status = wuff_buffer_clear(handle);
	WUFF_STATUS_BAIL()

	return	WUFF_SUCCESS;
}

wuff_sint32 wuff_tell(struct wuff_handle * handle, wuff_uint64 * offset)
{
	if (handle == NULL)
		return WUFF_INVALID_PARAM;

	*offset = handle->stream.position;

	return	WUFF_SUCCESS;
}

wuff_sint32 wuff_stream_info(struct wuff_handle * handle, struct wuff_info * info)
{
	if (handle == NULL || info == NULL)
		return WUFF_INVALID_PARAM;

	info->format = handle->stream.format;
	info->channels = handle->stream.header.channels;
	info->sample_rate = handle->stream.header.sample_rate;
	info->bits_per_sample = handle->stream.header.bits_per_sample;
	info->length = handle->stream.length;
	/* Think about adding channel mapping and perhaps other things. */

	return WUFF_SUCCESS;
}

wuff_sint32 wuff_format(struct wuff_handle * handle, wuff_uint16 format)
{
	wuff_sint32 wuff_status;

	if (handle == NULL)
		return WUFF_INVALID_PARAM;
	else if (format >= WUFF_FORMAT_MAX)
		return WUFF_FORMAT_UNSUPPORTED;

	/* A format change resets the position to the start of the block. */
	wuff_status = wuff_seek(handle, handle->stream.position);
	WUFF_STATUS_BAIL()

	wuff_status = wuff_set_output_format(handle, format);
	WUFF_STATUS_BAIL()

	return WUFF_SUCCESS;
}

wuff_sint32 wuff_read(struct wuff_handle * handle, wuff_uint8 * out_buffer, size_t * out_size)
{
	size_t current_offset;
	size_t r_samples, num_samples;
	wuff_uint8 head_offset, head, tail, sample_size;
	wuff_uint8 * in_buffer;
	wuff_sint32 wuff_status;

	if (handle == NULL || out_buffer == NULL || out_size == NULL)
		return WUFF_INVALID_PARAM;

	if (*out_size == 0)
		return WUFF_SUCCESS;

	sample_size = (wuff_uint8)handle->output.bytes_per_sample;

	/* Calculating the number of samples that fit into the application buffer. */
	/* The first and last sample may be truncated. */
	current_offset = handle->output.block_offset;
	head_offset = current_offset % sample_size;
	head = head_offset == 0 ? 0 : sample_size - head_offset;
	num_samples = wuff_calculate_samples(*out_size, sample_size, &head, &tail);

	/* Requesting the number of samples from the buffer. */
	/* Calculate the new sample count if necessary and write the output. */
	r_samples = num_samples;
	wuff_status = wuff_buffer_request(handle, &in_buffer, &r_samples);
	WUFF_STATUS_BAIL()
	else if (r_samples == 0)
	{
		/* Possible EOF. */
		*out_size = 0;
	}
	else
	{
		if (r_samples == 1 && head != 0)
		{
			/* Only the first truncated sample fits. */
			/* I really hope nobody will use small buffers like this. */
			num_samples = 0;
			tail = 0;
		}
		else
		{
			/* At this point the first (possibly truncated) sample will be fully written. */
			/* Subtract the first and last sample from the count if they're truncated. */
			if (r_samples < num_samples)
				tail = 0;
			num_samples = r_samples - !!head - !!tail;
		}

		handle->output.function(out_buffer, in_buffer, num_samples, head_offset, head, tail);

		/* Report the number of bytes written. */
		*out_size = num_samples * sample_size + head + tail;

		/* Adjust the block offset and sample position. */
		current_offset += *out_size;
		if (current_offset >= handle->output.block_size)
		{
			handle->stream.position += current_offset / handle->output.block_size;
			handle->output.block_offset = current_offset % handle->output.block_size;
		}
		else
		{
			handle->output.block_offset = current_offset;
		}

		/* Release the fully processed samples from the buffer. */
		wuff_status = wuff_buffer_release(handle, head_offset + head == sample_size ? num_samples + 1 : num_samples);
		WUFF_STATUS_BAIL()
	}

	return WUFF_SUCCESS;
}

void wuff_version(struct wuff_version * version)
{
	if (version == NULL)
		return;

	version->major = WUFF_VERSION_MAJOR;
	version->minor = WUFF_VERSION_MINOR;
	version->build = WUFF_VERSION_BUILD;
	version->revision = WUFF_VERSION_REVISION;
}