forked from mixxxdj/mixxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoundsourceopus.cpp
286 lines (253 loc) · 10.6 KB
/
soundsourceopus.cpp
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
#include "sources/soundsourceopus.h"
#include <QFileInfo>
#include "audio/streaminfo.h"
#include "util/logger.h"
namespace mixxx {
namespace {
const Logger kLogger("SoundSourceOpus");
// Decoded output of opusfile has a fixed sample rate of 48 kHz (fullband)
constexpr audio::SampleRate kSampleRate = audio::SampleRate(48000);
// http://opus-codec.org
// - Sample rate 48 kHz (fullband)
// - Frame sizes from 2.5 ms to 60 ms
// => Up to 48000 kHz * 0.06 s = 2880 sample frames per data frame
// Prefetching 2 * 2880 sample frames while seeking limits the decoding
// errors to kMaxDecodingError during our tests.
//
// According to the API documentation of op_pcm_seek():
// "...decoding after seeking may not return exactly the same
// values as would be obtained by decoding the stream straight
// through. However, such differences are expected to be smaller
// than the loss introduced by Opus's lossy compression."
// This implementation internally uses prefetching to compensate
// those differences, although not completely. The following
// constant indicates the maximum expected difference for
// testing purposes.
constexpr SINT kNumberOfPrefetchFrames = 2 * 2880;
// Parameter for op_channel_count()
// See also: https://mf4.xiph.org/jenkins/view/opus/job/opusfile-unix/ws/doc/html/group__stream__info.html
constexpr int kCurrentStreamLink = -1; // get ... of the current (stream) link
// Parameter for op_pcm_total() and op_bitrate()
// See also: https://mf4.xiph.org/jenkins/view/opus/job/opusfile-unix/ws/doc/html/group__stream__info.html
constexpr int kEntireStreamLink = -1; // get ... of the whole/entire stream
class OggOpusFileOwner {
public:
explicit OggOpusFileOwner(OggOpusFile* pFile)
: m_pFile(pFile) {
}
OggOpusFileOwner(OggOpusFileOwner&&) = delete;
OggOpusFileOwner(const OggOpusFileOwner&) = delete;
~OggOpusFileOwner() {
if (m_pFile) {
op_free(m_pFile);
}
}
operator OggOpusFile*() const {
return m_pFile;
}
OggOpusFile* release() {
OggOpusFile* pFile = m_pFile;
m_pFile = nullptr;
return pFile;
}
private:
OggOpusFile* m_pFile;
};
} // anonymous namespace
//static
const QString SoundSourceProviderOpus::kDisplayName = QStringLiteral("Xiph.org libopusfile");
//static
const QStringList SoundSourceProviderOpus::kSupportedFileTypes = {
QStringLiteral("opus"),
};
SoundSourceProviderPriority SoundSourceProviderOpus::getPriorityHint(
const QString& supportedFileType) const {
Q_UNUSED(supportedFileType)
// This reference decoder is supposed to produce more accurate
// and reliable results than any other DEFAULT provider.
return SoundSourceProviderPriority::Higher;
}
SoundSourceOpus::SoundSourceOpus(const QUrl& url)
: SoundSource(url),
m_pOggOpusFile(nullptr),
m_curFrameIndex(0) {
}
SoundSourceOpus::~SoundSourceOpus() {
close();
}
SoundSource::OpenResult SoundSourceOpus::tryOpen(
OpenMode /*mode*/,
const OpenParams& params) {
// From opus/opusfile.h
// On Windows, this string must be UTF-8 (to allow access to
// files whose names cannot be represented in the current
// MBCS code page).
// All other systems use the native character encoding.
#ifdef _WIN32
QByteArray qBAFilename = getLocalFileName().toUtf8();
#else
QByteArray qBAFilename = QFile::encodeName(getLocalFileName());
#endif
int errorCode = 0;
OggOpusFileOwner pOggOpusFile(
op_open_file(qBAFilename.constData(), &errorCode));
if (!pOggOpusFile || (errorCode != 0)) {
kLogger.warning()
<< "Opening of OggOpusFile failed with error"
<< errorCode
<< ":"
<< getLocalFileName();
return OpenResult::Failed;
}
if (!op_seekable(pOggOpusFile)) {
kLogger.warning()
<< "Stream in"
<< getUrlString()
<< "is not seekable";
return OpenResult::Aborted;
}
DEBUG_ASSERT(!m_pOggOpusFile);
m_pOggOpusFile = pOggOpusFile.release();
const int streamChannelCount = op_channel_count(m_pOggOpusFile, kCurrentStreamLink);
if (0 < streamChannelCount) {
// opusfile supports to enforce stereo decoding
bool enforceStereoDecoding =
params.getSignalInfo().getChannelCount().isValid() &&
(params.getSignalInfo().getChannelCount() <= 2) &&
// preserve mono signals if stereo signal is not requested explicitly
((params.getSignalInfo().getChannelCount() == 2) || (streamChannelCount > 2));
if (enforceStereoDecoding) {
initChannelCountOnce(2);
} else {
initChannelCountOnce(streamChannelCount);
}
} else {
kLogger.warning()
<< "Failed to read channel configuration of OggOpus file:"
<< getUrlString();
return OpenResult::Failed;
}
// Reserve enough capacity for buffering a stereo signal!
const auto prefetchChannelCount = std::min(getSignalInfo().getChannelCount(), audio::ChannelCount(2));
SampleBuffer(prefetchChannelCount * kNumberOfPrefetchFrames).swap(m_prefetchSampleBuffer);
const ogg_int64_t pcmTotal = op_pcm_total(m_pOggOpusFile, kEntireStreamLink);
if (0 <= pcmTotal) {
initFrameIndexRangeOnce(IndexRange::forward(0, pcmTotal));
} else {
kLogger.warning()
<< "Failed to read total length of OggOpus file:"
<< getUrlString();
return OpenResult::Failed;
}
const opus_int32 bitrate = op_bitrate(m_pOggOpusFile, kEntireStreamLink);
if (0 < bitrate) {
initBitrateOnce(bitrate / 1000);
} else {
kLogger.warning()
<< "Failed to determine bitrate of OggOpus file:"
<< getUrlString();
return OpenResult::Failed;
}
initSampleRateOnce(kSampleRate);
m_curFrameIndex = frameIndexMin();
return OpenResult::Succeeded;
}
void SoundSourceOpus::close() {
if (m_pOggOpusFile) {
op_free(m_pOggOpusFile);
m_pOggOpusFile = nullptr;
}
}
ReadableSampleFrames SoundSourceOpus::readSampleFramesClamped(
const WritableSampleFrames& writableSampleFrames) {
const SINT firstFrameIndex = writableSampleFrames.frameIndexRange().start();
if (m_curFrameIndex != firstFrameIndex) {
// Prefer skipping over seeking if the seek position is up to
// 2 * kNumberOfPrefetchFrames in front of the current position
if ((m_curFrameIndex > firstFrameIndex) ||
((firstFrameIndex - m_curFrameIndex) > 2 * kNumberOfPrefetchFrames)) {
SINT seekIndex = std::max(firstFrameIndex - kNumberOfPrefetchFrames, frameIndexMin());
int seekResult = op_pcm_seek(m_pOggOpusFile, seekIndex);
if (0 == seekResult) {
m_curFrameIndex = seekIndex;
} else {
kLogger.warning() << "Failed to seek OggOpus file:" << seekResult;
const ogg_int64_t pcmOffset = op_pcm_tell(m_pOggOpusFile);
if (0 <= pcmOffset) {
m_curFrameIndex = pcmOffset;
} else {
// Reset to EOF
m_curFrameIndex = frameIndexMax();
}
// Abort
return ReadableSampleFrames(
IndexRange::between(
m_curFrameIndex,
m_curFrameIndex));
}
}
// Decoding starts before the actual target position
// -> skip decoded samples until reaching the target position
DEBUG_ASSERT(m_curFrameIndex <= firstFrameIndex);
const auto precedingFrames =
IndexRange::between(m_curFrameIndex, firstFrameIndex);
if (!precedingFrames.empty() && (precedingFrames != readSampleFramesClamped(WritableSampleFrames(precedingFrames)).frameIndexRange())) {
kLogger.warning()
<< "Failed to skip preceding frames"
<< precedingFrames;
return ReadableSampleFrames(IndexRange::between(m_curFrameIndex, m_curFrameIndex));
}
}
DEBUG_ASSERT(m_curFrameIndex == firstFrameIndex);
const SINT numberOfFramesTotal = writableSampleFrames.frameLength();
// pSampleBuffer might be null while skipping (see above)
CSAMPLE* pSampleBuffer = writableSampleFrames.writableData();
SINT numberOfFramesRemaining = numberOfFramesTotal;
while (0 < numberOfFramesRemaining) {
SINT numberOfSamplesToRead =
getSignalInfo().frames2samples(numberOfFramesRemaining);
if (!writableSampleFrames.writableData()) {
// NOTE(uklotzde): The opusfile API does not provide any
// functions for skipping samples in the audio stream. Calling
// API functions with a nullptr buffer does not return. Since
// seeking in Opus files requires prefetching + skipping we
// need to skip sample frames by reading into a temporary
// buffer
pSampleBuffer = m_prefetchSampleBuffer.data();
if (numberOfSamplesToRead > m_prefetchSampleBuffer.size()) {
numberOfSamplesToRead = m_prefetchSampleBuffer.size();
}
}
int readResult;
if (getSignalInfo().getChannelCount() == 2) {
readResult = op_read_float_stereo(
m_pOggOpusFile,
pSampleBuffer,
numberOfSamplesToRead);
} else {
readResult = op_read_float(
m_pOggOpusFile,
pSampleBuffer,
numberOfSamplesToRead,
nullptr);
}
if (0 < readResult) {
m_curFrameIndex += readResult;
pSampleBuffer += getSignalInfo().frames2samples(readResult);
numberOfFramesRemaining -= readResult;
} else {
kLogger.warning() << "Failed to read sample data from OggOpus file:"
<< readResult;
break; // abort
}
}
DEBUG_ASSERT(isValidFrameIndex(m_curFrameIndex));
DEBUG_ASSERT(numberOfFramesTotal >= numberOfFramesRemaining);
const SINT numberOfFrames = numberOfFramesTotal - numberOfFramesRemaining;
return ReadableSampleFrames(
IndexRange::forward(firstFrameIndex, numberOfFrames),
SampleBuffer::ReadableSlice(
writableSampleFrames.writableData(),
std::min(writableSampleFrames.writableLength(), getSignalInfo().frames2samples(numberOfFrames))));
}
} // namespace mixxx