OpenTTD Source 20241224-master-gee860a5c8e
soundloader_opus.cpp
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD 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, version 2.
4 * OpenTTD 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.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#include "stdafx.h"
12#include "sound_type.h"
13#include "soundloader_type.h"
14
15#include <opusfile.h>
16
17#include "safeguards.h"
18
21public:
22 SoundLoader_Opus() : SoundLoader("opus", "Opus sound loader", 10) {}
23
24 static constexpr uint16_t OPUS_SAMPLE_RATE = 48000;
25 static constexpr uint8_t OPUS_SAMPLE_BITS = 16;
26
27 /* For good results, you will need at least 57 bytes (for a pure Opus-only stream). */
28 static constexpr size_t MIN_OPUS_FILE_SIZE = 57U;
29
30 /* It is recommended that this be large enough for at least 120 ms of data at 48 kHz per channel (5760 values per channel).
31 * Smaller buffers will simply return less data, possibly consuming more memory to buffer the data internally. */
32 static constexpr size_t DECODE_BUFFER_SAMPLES = 5760 * 2;
33 static constexpr size_t DECODE_BUFFER_BYTES = DECODE_BUFFER_SAMPLES * sizeof(opus_int16);
34
35 bool Load(SoundEntry &sound, bool new_format, std::vector<uint8_t> &data) override
36 {
37 if (!new_format) return false;
38
39 /* At least 57 bytes are needed for an Opus-only file. */
40 if (sound.file_size < MIN_OPUS_FILE_SIZE) return false;
41
42 /* Test if data is an Ogg Opus stream, as identified by the initial file header. */
43 auto filepos = sound.file->GetPos();
44 std::vector<uint8_t> tmp(MIN_OPUS_FILE_SIZE);
45 sound.file->ReadBlock(tmp.data(), tmp.size());
46 if (op_test(nullptr, tmp.data(), tmp.size()) != 0) return false;
47
48 /* Read the whole file into memory. */
49 tmp.resize(sound.file_size);
50 sound.file->SeekTo(filepos, SEEK_SET);
51 sound.file->ReadBlock(tmp.data(), tmp.size());
52
53 int error = 0;
54 auto of = std::unique_ptr<OggOpusFile, OggOpusFileDeleter>(op_open_memory(tmp.data(), tmp.size(), &error));
55 if (error != 0) return false;
56
57 size_t datapos = 0;
58 for (;;) {
59 data.resize(datapos + DECODE_BUFFER_BYTES);
60
61 int link_index;
62 int read = op_read(of.get(), reinterpret_cast<opus_int16 *>(&data[datapos]), DECODE_BUFFER_BYTES, &link_index);
63 if (read == 0) break;
64
65 if (read < 0 || op_channel_count(of.get(), link_index) != 1) {
66 /* Error reading, or incorrect channel count. */
67 data.clear();
68 return false;
69 }
70
71 datapos += read * sizeof(opus_int16);
72 }
73
74 /* OpusFile always decodes at 48kHz. */
75 sound.channels = 1;
76 sound.bits_per_sample = OPUS_SAMPLE_BITS;
77 sound.rate = OPUS_SAMPLE_RATE;
78
79 /* We resized by DECODE_BUFFER_BYTES just before finally reading zero bytes, undo this. */
80 data.resize(data.size() - DECODE_BUFFER_BYTES);
81
82 return true;
83 }
84
85private:
88 void operator()(OggOpusFile *of)
89 {
90 if (of != nullptr) op_free(of);
91 }
92 };
93};
94
95static SoundLoader_Opus s_sound_loader_opus;
void ReadBlock(void *ptr, size_t size)
Read a block.
size_t GetPos() const
Get position in the file.
void SeekTo(size_t pos, int mode)
Seek in the current file.
Opus sound loader.
static constexpr uint16_t OPUS_SAMPLE_RATE
OpusFile always decodes at 48kHz.
static constexpr uint8_t OPUS_SAMPLE_BITS
OpusFile op_read() uses 16 bits per sample.
Base interface for a SoundLoader implementation.
Class related to random access to files.
A number of safeguards to prevent using unsafe methods.
Types related to sounds.
Types related to sound loaders.
Definition of base types and functions in a cross-platform compatible way.
Helper class to RAII release an OggOpusFile.