File: ogg.cpp

package info (click to toggle)
0ad 0.28.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 182,352 kB
  • sloc: cpp: 201,989; javascript: 19,730; ansic: 15,057; python: 6,597; sh: 2,046; perl: 1,232; xml: 543; java: 533; makefile: 105
file content (281 lines) | stat: -rw-r--r-- 6,470 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
/* Copyright (C) 2025 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. 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.
 *
 * 0 A.D. 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 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "precompiled.h"

#include "ogg.h"

#if CONFIG2_AUDIO

#include "lib/byte_order.h"
#include "lib/code_annotation.h"
#include "lib/debug.h"
#include "lib/file/io/io.h"
#include "lib/posix/posix_types.h"
#include "maths/MathUtil.h"
#include "ps/CLogger.h"

#include <AL/al.h>
#include <algorithm>
#include <fcntl.h>
#include <fmt/format.h>
#include <iterator>
#include <ogg/config_types.h>
#include <stdexcept>
#include <vorbis/codec.h>
#include <vorbis/vorbisfile.h>

static Status LibErrorFromVorbis(int err)
{
	switch(err)
	{
	case 0:
		return INFO::OK;
	case OV_HOLE:
		return ERR::AGAIN;
	case OV_EREAD:
		return ERR::IO;
	case OV_EFAULT:
		return ERR::LOGIC;
	case OV_EIMPL:
		return ERR::NOT_SUPPORTED;
	case OV_EINVAL:
		return ERR::INVALID_PARAM;
	case OV_ENOTVORBIS:
		return ERR::NOT_SUPPORTED;
	case OV_EBADHEADER:
		return ERR::CORRUPTED;
	case OV_EVERSION:
		return ERR::INVALID_VERSION;
	case OV_ENOTAUDIO:
		return ERR::_1;
	case OV_EBADPACKET:
		return ERR::_2;
	case OV_EBADLINK:
		return ERR::_3;
	case OV_ENOSEEK:
		return ERR::_4;
	default:
		return ERR::FAIL;
	}
}

//-----------------------------------------------------------------------------

class VorbisBufferAdapter
{
public:
	VorbisBufferAdapter(const std::shared_ptr<u8>& buffer, size_t size)
		: m_Buffer(buffer)
		, m_Size(size)
		, m_Offset(0)
	{
	}

	static size_t Read(void* bufferToFill, size_t itemSize, size_t numItems, void* context)
	{
		VorbisBufferAdapter* adapter{static_cast<VorbisBufferAdapter*>(context)};

		const off_t sizeRequested{static_cast<off_t>(numItems * itemSize)};
		const off_t sizeRemaining{adapter->m_Size - adapter->m_Offset};
		const size_t sizeToRead{static_cast<size_t>(std::min(sizeRequested, sizeRemaining))};

		std::copy_n(
			adapter->m_Buffer.get() + adapter->m_Offset,
			sizeToRead,
			static_cast<u8*>(bufferToFill)
		);

		adapter->m_Offset += sizeToRead;
		return sizeToRead;
	}

	static int Seek(void* context, ogg_int64_t offset, int whence)
	{
		VorbisBufferAdapter* adapter{static_cast<VorbisBufferAdapter*>(context)};

		off_t origin{0};
		switch(whence)
		{
		case SEEK_SET:
			origin = 0;
			break;
		case SEEK_CUR:
			origin = adapter->m_Offset;
			break;
		case SEEK_END:
			origin = adapter->m_Size + 1;
			break;
			NODEFAULT;
		}

		adapter->m_Offset = Clamp(static_cast<off_t>(origin + offset), static_cast<off_t>(0), adapter->m_Size);
		return 0;
	}

	static int Close(void* context)
	{
		VorbisBufferAdapter* adapter{static_cast<VorbisBufferAdapter*>(context)};
		adapter->m_Buffer.reset();
		return 0;	// return value is ignored
	}

	static long Tell(void* context)
	{
		VorbisBufferAdapter* adapter{static_cast<VorbisBufferAdapter*>(context)};
		return adapter->m_Offset;
	}

private:
	std::shared_ptr<u8> m_Buffer;
	off_t m_Size;
	off_t m_Offset;
};


//-----------------------------------------------------------------------------

template <typename Adapter>
class OggStreamImpl : public OggStream
{
public:
	OggStreamImpl(const Adapter& adapter)
		: m_Adapter(adapter),
		m_FileEOF(false),
		m_Info(nullptr)
	{
		Open();
	}

	~OggStreamImpl()
	{
		ov_clear(&m_VorbisFile);
	}

	virtual ALenum Format()
	{
		return (m_Info->channels == 1)? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
	}

	virtual ALsizei SamplingRate()
	{
		return m_Info->rate;
	}
	virtual bool AtFileEOF()
	{
		return m_FileEOF;
	}

	virtual Status ResetFile()
	{
	    ov_time_seek(&m_VorbisFile, 0);
	    m_FileEOF = false;
		return INFO::OK;
	}

	virtual size_t GetNextChunk(std::span<u8> buffer)
	{
		// We may have to call ov_read multiple times because it
		// treats the buffer size "as a limit and not a request".
		size_t bytesRead{0};
		while (bytesRead < buffer.size())
		{
			constexpr int isBigEndian{(BYTE_ORDER == BIG_ENDIAN)};
			constexpr int wordSize{sizeof(i16)};
			constexpr int isSigned{1};
			// Unused.
			int bitstream;
			const int ret{static_cast<int>(ov_read(
				&m_VorbisFile,
				reinterpret_cast<char*>(buffer.data() + bytesRead),
				static_cast<int>(buffer.size() - bytesRead),
				isBigEndian,
				wordSize,
				isSigned,
				&bitstream
			))};
			if(ret == 0) {
				m_FileEOF = true;
				return bytesRead;
			}

			if(ret < 0)
				WARN_RETURN(LibErrorFromVorbis(ret));

			bytesRead += ret;
		}
		return bytesRead;
	}

private:
	void Open()
	{
		ov_callbacks callbacks;
		callbacks.read_func = Adapter::Read;
		callbacks.close_func = Adapter::Close;
		callbacks.seek_func = Adapter::Seek;
		callbacks.tell_func = Adapter::Tell;
		const int ret{ov_open_callbacks(&m_Adapter, &m_VorbisFile, 0, 0, callbacks)};
		switch (ret)
		{
			case 0:
				break;
			case OV_EBADHEADER:
			case OV_EREAD:
			case OV_ENOTVORBIS:
			case OV_EVERSION:
				throw std::runtime_error(fmt::format("Failed to open Ogg Vorbis file: {}", LibErrorFromVorbis(ret)));
			default:
				throw std::runtime_error(fmt::format("Unknown error opening Ogg Vorbis file: {}", LibErrorFromVorbis(ret)));
		}

		// Retrieve info for current bitstream.
		const int link{-1};
		m_Info = ov_info(&m_VorbisFile, link);
		if(!m_Info)
			DEBUG_WARN_ERR(ERR::INVALID_HANDLE);
	}

	Adapter m_Adapter;
	OggVorbis_File m_VorbisFile;
	vorbis_info* m_Info;
	bool m_FileEOF;
};


//-----------------------------------------------------------------------------

Status OpenOggNonstream(const PIVFS& vfs, const VfsPath& pathname, OggStreamPtr& stream)
{
	std::shared_ptr<u8> contents;
	size_t size;
	RETURN_STATUS_IF_ERR(vfs->LoadFile(pathname, contents, size));

	try
	{
		std::shared_ptr<OggStreamImpl<VorbisBufferAdapter>> tmp{std::make_shared<OggStreamImpl<VorbisBufferAdapter>>(VorbisBufferAdapter(contents, size))};
		stream = tmp;
		return INFO::OK;
	}
	catch(const std::runtime_error& e)
	{
		LOGERROR(e.what());
		return ERR::IO;
	}
}

#endif	// CONFIG2_AUDIO