File: oggopus.cpp

package info (click to toggle)
warzone2100 4.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,320 kB
  • sloc: cpp: 676,209; ansic: 391,201; javascript: 78,238; python: 16,632; php: 4,294; sh: 4,094; makefile: 2,629; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (391 lines) | stat: -rw-r--r-- 12,051 bytes parent folder | download | duplicates (2)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
	This file is part of Warzone 2100.
	Copyright (C) 2005-2022  Warzone 2100 Project

	Warzone 2100 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.

	Warzone 2100 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 Warzone 2100; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include "lib/framework/frame.h"
#include "oggopus.h"
#include <physfs.h>
#include <limits>
#include <algorithm>
#include "lib/framework/physfs_ext.h"
#include "lib/framework/file.h"

// MARK: - Opus PHYSFS callbacks

static int wz_opus_physfs_read(void *datasource, unsigned char *_ptr, int _nbytes)
{
	PHYSFS_file *fileHandle;
	ASSERT(datasource != nullptr, "NULL decoder passed!");

	fileHandle = ((WzAudioDataPhysFSHandle *)datasource)->fileHandle();
	ASSERT(fileHandle != nullptr, "Bad PhysicsFS file handle passed in");

	ASSERT(_nbytes <= static_cast<size_t>(std::numeric_limits<PHYSFS_uint32>::max()), "readLen (%i) exceeds PHYSFS_uint32::max", _nbytes);
	const int32_t didread = WZ_PHYSFS_readBytes(fileHandle, _ptr, static_cast<PHYSFS_uint32>(_nbytes));
	return didread;
}

static int wz_opus_physfs_seek(void *datasource, opus_int64 offset, int whence)
{
	PHYSFS_file *fileHandle;
	opus_int64 newPos;
	ASSERT(datasource != nullptr, "NULL decoder passed!");
	fileHandle = ((WzAudioDataPhysFSHandle *)datasource)->fileHandle();
	ASSERT(fileHandle != nullptr, "Bad PhysicsFS file handle passed in");

	switch (whence)
	{
	// Seek to absolute position
	case SEEK_SET:
		newPos = offset;
		break;

	// Seek `offset` ahead
	case SEEK_CUR:
		{
			int64_t curPos = PHYSFS_tell(fileHandle);
			if (curPos == -1)
			{
				return -1;
			}

			newPos = curPos + offset;
			break;
		}

	// Seek backwards from the end of the file
	case SEEK_END:
		{
			int64_t fileSize = PHYSFS_fileLength(fileHandle);
			if (fileSize == -1)
			{
				return -1;
			}

			newPos = fileSize - 1 - offset;
			break;
		}

	// unrecognized seek instruction
	default:
		// indicate failure
		return -1;
	}

	// PHYSFS_seek return value of non-zero means success
	if (PHYSFS_seek(fileHandle, newPos) != 0)
	{
	 // success is zero! opposite of what PHYSFS thinks
		return 0;
	}
	else
	{
		// indicate failure
		return -1;    // failure
	}
}

static opus_int64 wz_opus_physfs_tell(void *datasource)
{
	PHYSFS_file *fileHandle;
	ASSERT(datasource != nullptr, "NULL decoder passed!");

	fileHandle = ((WzAudioDataPhysFSHandle *)datasource)->fileHandle();
	ASSERT(fileHandle != nullptr, "Bad PhysicsFS file handle passed in");

	const auto out = PHYSFS_tell(fileHandle);
	return static_cast<opus_int64>(out);
}

// https://github.com/xiph/opusfile
// https://opus-codec.org/docs/opusfile_api-0.12/index.html
const OpusFileCallbacks wz_oggOpus_physfs_callbacks =
{
	wz_opus_physfs_read,
	// this one must return 0 on success, unlike its libvorbis counterpart
	wz_opus_physfs_seek,
	wz_opus_physfs_tell,
	nullptr,
};

// MARK: - Opus memory buffer callbacks

static int wz_opus_membuf_read(void *datasource, unsigned char *_ptr, int _nbytes)
{
	WZAudioDataMemoryBuffer *pMemBuf;
	ASSERT(datasource != nullptr, "NULL decoder passed!");
	pMemBuf = (WZAudioDataMemoryBuffer *)datasource;

	if (_nbytes < 0)
	{
		return -1;
	}
	size_t bytesToRead = std::min<size_t>(static_cast<size_t>(_nbytes), pMemBuf->buffer.size() - pMemBuf->currPos);
	if (bytesToRead == 0)
	{
		return 0;
	}
	memcpy(_ptr, &(pMemBuf->buffer[pMemBuf->currPos]), bytesToRead);
	pMemBuf->currPos += static_cast<size_t>(bytesToRead);
	return static_cast<int>(bytesToRead);
}

static int wz_opus_membuf_seek(void *datasource, opus_int64 offset, int whence)
{
	WZAudioDataMemoryBuffer *pMemBuf;
	opus_int64 newPos;
	ASSERT(datasource != nullptr, "NULL decoder passed!");
	pMemBuf = ((WZAudioDataMemoryBuffer *)datasource);

	switch (whence)
	{
	// Seek to absolute position
	case SEEK_SET:
		newPos = offset;
		break;

	// Seek `offset` ahead
	case SEEK_CUR:
		{
			if (pMemBuf->error)
			{
				return -1;
			}
			newPos = static_cast<int64_t>(pMemBuf->currPos) + offset;
			break;
		}

	// Seek backwards from the end of the file
	case SEEK_END:
		{
			if (pMemBuf->error)
			{
				return -1;
			}
			newPos = static_cast<int64_t>(pMemBuf->buffer.size()) - 1 - offset;
			break;
		}

	// unrecognized seek instruction
	default:
		// indicate failure
		return -1;
	}

	if (newPos < 0)
	{
		return -1; // failure
	}
	if (newPos >= static_cast<int64_t>(pMemBuf->buffer.size()))
	{
		return -1; // failure
	}

	pMemBuf->currPos = static_cast<size_t>(newPos);
	return 0; // success
}

static opus_int64 wz_opus_membuf_tell(void *datasource)
{
	WZAudioDataMemoryBuffer *pMemBuf;
	ASSERT(datasource != nullptr, "NULL decoder passed!");
	pMemBuf = ((WZAudioDataMemoryBuffer *)datasource);
	return static_cast<opus_int64>(pMemBuf->currPos);
}

// https://github.com/xiph/opusfile
// https://opus-codec.org/docs/opusfile_api-0.12/index.html
const OpusFileCallbacks wz_oggOpus_membuf_callbacks =
{
	wz_opus_membuf_read,
	// this one must return 0 on success, unlike its libvorbis counterpart
	wz_opus_membuf_seek,
	wz_opus_membuf_tell,
	nullptr,
};

// MARK: - WzOpusDecoder

WZOpusDecoder* WZOpusDecoder::fromFilename(const char* fileName, bool bufferEntireStream)
{
	WZOpusDecoder* result = nullptr;

	if (!bufferEntireStream)
	{
		PHYSFS_file *fileHandle = PHYSFS_openRead(fileName);
		debug(LOG_WZ, "Reading...[directory: %s] %s", WZ_PHYSFS_getRealDir_String(fileName).c_str(), fileName);
		if (fileHandle == nullptr)
		{
			debug(LOG_ERROR, "sound_LoadTrackFromFile: PHYSFS_openRead(\"%s\") failed with error: %s\n", fileName, WZ_PHYSFS_getLastError());
			return nullptr;
		}

		auto data = std::make_unique<WzAudioDataPhysFSHandle>(fileHandle, fileName);
		result = openOpusInternal(std::move(data), &wz_oggOpus_physfs_callbacks);
	}
	else
	{
		auto data = std::make_unique<WZAudioDataMemoryBuffer>();
		if (!loadFileToBufferVector(fileName, data->buffer, false, false))
		{
			debug(LOG_ERROR, "sound_LoadTrackFromFile: Failed to open / read file contents: %s\n", fileName);
			return nullptr;
		}
		result = openOpusInternal(std::move(data), &wz_oggOpus_membuf_callbacks);
	}

	if (!result)
	{
		debug(LOG_ERROR, "OP failed to create decoder for %s", fileName);
	}
	return result;
}

WZOpusDecoder* WZOpusDecoder::openOpusInternal(std::unique_ptr<WZAudioDataResourceInterface> data, const OpusFileCallbacks *cb)
{
	int error = 0;
	OggOpusFile *of = op_open_callbacks(data.get(), cb, nullptr, 0, &error);
	switch (error)
	{
	case OP_EREAD:
		debug(LOG_ERROR, "An underlying read, seek, or tell operation failed when it should have succeeded, or we failed to find data in the stream we had seen before.");
		return nullptr;
	case OP_EFAULT:
		debug(LOG_ERROR, "There was a memory allocation failure, or an internal library error.");
		return nullptr;
	case OP_EIMPL:
		debug(LOG_ERROR, "The stream used a feature that is not implemented, such as an unsupported channel family.");
		return nullptr;
	case OP_EINVAL:
		debug(LOG_ERROR, "seek() was implemented and succeeded on this source, but tell() did not, or the starting position indicator was not equal to _initial_bytes.");
		return nullptr;
	case OP_ENOTFORMAT:
		debug(LOG_ERROR, "The stream contained a link that did not have any logical Opus streams in it.");
		return nullptr;
	case OP_EBADHEADER:
		debug(LOG_ERROR, "A required header packet was not properly formatted, contained illegal values, or was missing altogether.");
		return nullptr;
	case OP_EVERSION:
		debug(LOG_ERROR, "An ID header contained an unrecognized version number.");
		return nullptr;
	case OP_EBADLINK:
		debug(LOG_ERROR, "We failed to find data we had seen before after seeking.");
		return nullptr;
	case OP_EBADTIMESTAMP:
		debug(LOG_ERROR, "The first or last timestamp in a link failed basic validity checks.");
		return nullptr;
	default:
		break;
	}
	ASSERT(error >= 0, "Unexpected libopusfile error %i", error);

	const OpusHead* head = op_head(of, -1);
	if (head == nullptr)
	{
		debug(LOG_ERROR, "OP failed to read header");
		op_free(of);
		return nullptr;
	}
	WZOpusDecoder *decoder = new WZOpusDecoder(std::move(data), of, head);
	// Use a negative number to get the ID header information of the current link
	if (decoder == nullptr)
	{
		op_free(of);
		return nullptr;
	}
	return decoder;
}

WZOpusDecoder* WZOpusDecoder::fromMemoryBuffer(std::unique_ptr<WZAudioDataMemoryBuffer> data)
{
	auto decoder = openOpusInternal(std::move(data), &wz_oggOpus_membuf_callbacks);
	if (!decoder)
	{
		debug(LOG_ERROR, "OP failed to create decoder for memory buffer");
	}
	return decoder;
}

optional<size_t> WZOpusDecoder::decode(uint8_t* buffer, size_t bufferSize)
{
	// we need a small buffer to convert from big to little endian
	// sample_rate (48000/sec) * depth (2 bytes) * 120 milliseconds
	constexpr size_t TMP_BUF = 48 * 2 * 120;
	opus_int16 pcm[TMP_BUF];

	size_t		bufferOffset = 0;
	int		    samples_per_chan = 0;
	m_bufferSize = bufferSize;
	// Decode PCM data into the buffer until there is nothing to decode left OR untill we hit bufferSize limit
	do
	{
		size_t spaceLeft = bufferSize - bufferOffset;
		ASSERT(spaceLeft <= static_cast<size_t>(std::numeric_limits<int>::max()), "spaceLeft (%zu) exceeds int::max", spaceLeft);
		int toRead = static_cast<int>(std::min<size_t>(TMP_BUF, spaceLeft));

		// Note: the return value is the number of *samples per channel*, not *bytes* !!
		samples_per_chan = op_read_stereo(m_of, pcm, toRead);
		switch (samples_per_chan)
		{
		case OP_HOLE:
			debug(LOG_ERROR, "There was a hole in the data, and some samples may have been skipped. Call this function again to continue decoding past the hole.");
			return nullopt;
		case OP_EREAD:
			debug(LOG_ERROR, "An underlying read operation failed. This may signal a truncation attack from an <https:> source.");
			return nullopt;
		case OP_EFAULT:
			debug(LOG_ERROR, "An internal memory allocation failed.");
			return nullopt;
		case OP_EIMPL:
			debug(LOG_ERROR, "An unseekable stream encountered a new link that used a feature that is not implemented, such as an unsupported channel family.");
			return nullopt;
		case OP_EINVAL:
			debug(LOG_ERROR, "The stream was only partially open.");
			return nullopt;
		case OP_ENOTFORMAT:
			debug(LOG_ERROR, "An unseekable stream encountered a new link that did not have any logical Opus streams in it. ");
			return nullopt;
		case OP_EBADHEADER:
			debug(LOG_ERROR, "An unseekable stream encountered a new link with a required header packet that was not properly formatted, contained illegal values, or was missing altogether.");
			return nullopt;
		case OP_EVERSION:
			debug(LOG_ERROR, "An unseekable stream encountered a new link with an ID header that contained an unrecognized version number.");
			return nullopt;
		case OP_EBADPACKET:
			debug(LOG_ERROR, "Failed to properly decode the next packet.");
			return nullopt;
		case OP_EBADLINK:
			debug(LOG_ERROR, "We failed to find data we had seen before.");
			return nullopt;
		case OP_EBADTIMESTAMP:
			debug(LOG_ERROR, "An unseekable stream encountered a new link with a starting timestamp that failed basic validity checks.");
			return nullopt;
		default:
			break;
		}
		ASSERT(samples_per_chan >= 0, "Unexpected Opus error %i", samples_per_chan);
		// convert to little endian
		for (int si = 0; si < m_nchannels * samples_per_chan; si++)
		{
			buffer[bufferOffset + 0] = (uint8_t)(pcm[si] & 0xFF);
			buffer[bufferOffset + 1] = (uint8_t)(pcm[si] >> 8 & 0xFF);
			bufferOffset +=2;
		}
	} while ((samples_per_chan != 0 && bufferOffset < std::min(bufferSize, TMP_BUF)));
	return bufferOffset;
}