OpenTTD Source: src/soundloader_opus.cpp Source File

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "stdafx.h"
11
12#include "debug.h"
13#include "misc/autorelease.hpp"
14#include "random_access_file_type.h"
15#include "sound_type.h"
16#include "soundloader_type.h"
17
18#include <opusfile.h>
19
20#include "safeguards.h"
21
23 class SoundLoader_Opus : public SoundLoader {
24public:
25 SoundLoader_Opus() : SoundLoader("opus", "Opus sound loader", 10) {}
26
27 static constexpr uint16_t OPUS_SAMPLE_RATE = 48000;
28 static constexpr uint8_t OPUS_SAMPLE_BITS = 16;
29
30 /* For good results, you will need at least 57 bytes (for a pure Opus-only stream). */
31 static constexpr size_t MIN_OPUS_FILE_SIZE = 57U;
32
33 /* It is recommended that this be large enough for at least 120 ms of data at 48 kHz per channel (5760 values per channel).
34 * Smaller buffers will simply return less data, possibly consuming more memory to buffer the data internally. */
35 static constexpr size_t DECODE_BUFFER_SAMPLES = 5760 * 2;
36 static constexpr size_t DECODE_BUFFER_BYTES = DECODE_BUFFER_SAMPLES * sizeof(opus_int16);
37
38 bool Load(SoundEntry &sound, bool new_format, std::vector<std::byte> &data) const override
39 {
40 if (!new_format) return false;
41
42 /* At least 57 bytes are needed for an Opus-only file. */
43 if (sound.file_size < MIN_OPUS_FILE_SIZE) return false;
44
45 /* Test if data is an Ogg Opus stream, as identified by the initial file header. */
46 auto filepos = sound.file->GetPos();
47 std::vector<uint8_t> tmp(MIN_OPUS_FILE_SIZE);
48 sound.file->ReadBlock(tmp.data(), tmp.size());
49 if (op_test(nullptr, tmp.data(), tmp.size()) != 0) return false;
50
51 /* Read the whole file into memory. */
52 tmp.resize(sound.file_size);
53 sound.file->SeekTo(filepos, SEEK_SET);
54 sound.file->ReadBlock(tmp.data(), tmp.size());
55
56 int error = 0;
57 auto of = AutoRelease<OggOpusFile, op_free>(op_open_memory(tmp.data(), tmp.size(), &error));
58 if (error != 0) {
59 Debug(grf, 0, "SoundLoader_Opus: Unable to open stream.");
60 return false;
61 }
62
63 size_t datapos = 0;
64 for (;;) {
65 data.resize(datapos + DECODE_BUFFER_BYTES);
66
67 int link_index;
68 int read = op_read(of.get(), reinterpret_cast<opus_int16 *>(&data[datapos]), DECODE_BUFFER_BYTES, &link_index);
69 if (read == 0) break;
70
71 if (read < 0) {
72 Debug(grf, 0, "SoundLoader_Opus: Unexpected end of stream.");
73 data.clear();
74 return false;
75 }
76
77 int channels = op_channel_count(of.get(), link_index);
78 if (channels != 1) {
79 Debug(grf, 0, "SoundLoader_Opus: Unsupported channels {}, expected 1.", channels);
80 data.clear();
81 return false;
82 }
83
84 datapos += read * sizeof(opus_int16);
85 }
86
87 /* OpusFile always decodes at 48kHz. */
88 sound.channels = 1;
89 sound.bits_per_sample = OPUS_SAMPLE_BITS;
90 sound.rate = OPUS_SAMPLE_RATE;
91
92 /* We resized by DECODE_BUFFER_BYTES just before finally reading zero bytes, undo this. */
93 data.resize(data.size() - DECODE_BUFFER_BYTES);
94
95 return true;
96 }
97
98private:
99 static SoundLoader_Opus instance;
100};
101
102/* static */ SoundLoader_Opus SoundLoader_Opus::instance{};
autorelease.hpp
Helper for std::unique_ptr to use an arbitrary function as the deleter.
AutoRelease
std::unique_ptr< T, DeleterFromFunc< Tfunc > > AutoRelease
Specialisation of std::unique_ptr for objects which must be deleted by calling a function.
Definition autorelease.hpp:25
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:23
SoundLoader_Opus::OPUS_SAMPLE_RATE
static constexpr uint16_t OPUS_SAMPLE_RATE
OpusFile always decodes at 48kHz.
Definition soundloader_opus.cpp:27
SoundLoader_Opus::OPUS_SAMPLE_BITS
static constexpr uint8_t OPUS_SAMPLE_BITS
OpusFile op_read() uses 16 bits per sample.
Definition soundloader_opus.cpp:28
SoundLoader
Base interface for a SoundLoader implementation.
Definition soundloader_type.h:17
debug.h
Functions related to debugging.
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
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