OpenTTD Source: src/soundloader_opus.cpp Source File

OpenTTD Source 15.0-beta1

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"
11#include "random_access_file_type.h"
12#include "sound_type.h"
13#include "soundloader_type.h"
14
15#include <opusfile.h>
16
17#include "safeguards.h"
18
20 class SoundLoader_Opus : public SoundLoader {
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:
87 struct OggOpusFileDeleter {
88 void operator()(OggOpusFile *of)
89 {
90 if (of != nullptr) op_free(of);
91 }
92 };
93};
94
95static SoundLoader_Opus s_sound_loader_opus;
RandomAccessFile::ReadBlock
void ReadBlock(void *ptr, size_t size)
Read a block.
Definition random_access_file.cpp:145
RandomAccessFile::GetPos
size_t GetPos() const
Get position in the file.
Definition random_access_file.cpp:71
RandomAccessFile::SeekTo
void SeekTo(size_t pos, int mode)
Seek in the current file.
Definition random_access_file.cpp:90
SoundLoader_Opus
Opus sound loader.
Definition soundloader_opus.cpp:20
SoundLoader_Opus::OPUS_SAMPLE_RATE
static constexpr uint16_t OPUS_SAMPLE_RATE
OpusFile always decodes at 48kHz.
Definition soundloader_opus.cpp:24
SoundLoader_Opus::OPUS_SAMPLE_BITS
static constexpr uint8_t OPUS_SAMPLE_BITS
OpusFile op_read() uses 16 bits per sample.
Definition soundloader_opus.cpp:25
SoundLoader
Base interface for a SoundLoader implementation.
Definition soundloader_type.h:17
random_access_file_type.h
Class related to random access to files.
safeguards.h
A number of safeguards to prevent using unsafe methods.
sound_type.h
Types related to sounds.
soundloader_type.h
Types related to sound loaders.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
SoundEntry
Definition sound_type.h:19
SoundLoader_Opus::OggOpusFileDeleter
Helper class to RAII release an OggOpusFile.
Definition soundloader_opus.cpp:87