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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#define FORBIDDEN_SYMBOL_EXCEPTION_FILE
#include "audio/decoders/mpc.h"
#ifdef USE_MPCDEC
#ifdef USE_MPCDEC_OLD_API
#include <mpcdec/mpcdec.h>
#else
#include <mpc/mpcdec.h>
#endif
#include "common/debug.h"
#include "common/stream.h"
#include "common/textconsole.h"
#include "audio/audiostream.h"
namespace Audio {
// These are wrapper functions to allow using a SeekableReadStream object to
// provide data to the mpc_reader object.
#ifdef USE_MPCDEC_OLD_API
static mpc_int32_t read_stream(void *data, void *ptr, mpc_int32_t size) {
Common::SeekableReadStream *stream = (Common::SeekableReadStream *)data;
#else
static mpc_int32_t read_stream(mpc_reader *p_reader, void *ptr, mpc_int32_t size) {
Common::SeekableReadStream *stream = (Common::SeekableReadStream *)p_reader->data;
#endif
return stream->read(ptr, size);
}
/// Seeks to byte position offset.
#ifdef USE_MPCDEC_OLD_API
static mpc_bool_t seek_stream(void *data, mpc_int32_t offset) {
Common::SeekableReadStream *stream = (Common::SeekableReadStream *)data;
#else
static mpc_bool_t seek_stream(mpc_reader *p_reader, mpc_int32_t offset) {
Common::SeekableReadStream *stream = (Common::SeekableReadStream *)p_reader->data;
#endif
return stream->seek(offset);
}
/// Returns the current byte offset in the stream.
#ifdef USE_MPCDEC_OLD_API
static mpc_int32_t tell_stream(void *data) {
Common::SeekableReadStream *stream = (Common::SeekableReadStream *)data;
#else
static mpc_int32_t tell_stream(mpc_reader *p_reader) {
Common::SeekableReadStream *stream = (Common::SeekableReadStream *)p_reader->data;
#endif
return stream->pos();
}
/// Returns the total length of the source stream, in bytes.
#ifdef USE_MPCDEC_OLD_API
static mpc_int32_t get_size_stream(void *data) {
Common::SeekableReadStream *stream = (Common::SeekableReadStream *)data;
#else
static mpc_int32_t get_size_stream(mpc_reader *p_reader) {
Common::SeekableReadStream *stream = (Common::SeekableReadStream *)p_reader->data;
#endif
return stream->size();
}
/// True if the stream is a seekable stream.
#ifdef USE_MPCDEC_OLD_API
static mpc_bool_t canseek_stream(void *p_reader) {
return TRUE;
}
#else
static mpc_bool_t canseek_stream(mpc_reader *p_reader) {
return MPC_TRUE;
}
#endif
#pragma mark -
#pragma mark --- Musepack stream ---
#pragma mark -
class MPCStream : public SeekableAudioStream {
protected:
Common::DisposablePtr<Common::SeekableReadStream> _inStream;
bool _isStereo;
int _rate;
Timestamp _length;
mpc_reader _reader;
mpc_streaminfo _si;
#ifdef USE_MPCDEC_OLD_API
mpc_decoder _decoder;
#else
mpc_demux *_demux;
#endif
MPC_SAMPLE_FORMAT _bufferDec[MPC_DECODER_BUFFER_LENGTH];
uint16 _buffer[MPC_DECODER_BUFFER_LENGTH];
const uint16 *_bufferEnd;
const uint16 *_pos;
public:
// startTime / duration are in milliseconds
MPCStream(Common::SeekableReadStream *inStream, DisposeAfterUse::Flag dispose);
~MPCStream();
int readBuffer(int16 *buffer, const int numSamples) override;
bool endOfData() const override { return _pos >= _bufferEnd; }
bool isStereo() const override { return _isStereo; }
int getRate() const override { return _rate; }
bool seek(const Timestamp &where) override;
Timestamp getLength() const override { return _length; }
protected:
bool refill();
};
MPCStream::MPCStream(Common::SeekableReadStream *inStream, DisposeAfterUse::Flag dispose) :
_inStream(inStream, dispose),
_length(0, 1000),
_bufferEnd(ARRAYEND(_buffer)) {
_pos = _bufferEnd; // This will return endOfBuffer() if we're not properly inited
_reader.read = read_stream;
_reader.seek = seek_stream;
_reader.tell = tell_stream;
_reader.get_size = get_size_stream;
_reader.canseek = canseek_stream;
_reader.data = (void *)inStream;
#ifdef USE_MPCDEC_OLD_API
mpc_streaminfo_init(&_si);
if (mpc_streaminfo_read(&_si, &_reader) < 0) {
warning("Cannot read musepack stream info");
return;
}
mpc_decoder_setup(&_decoder, &_reader);
mpc_decoder_scale_output (&_decoder, 1.0);
if (!mpc_decoder_initialize(&_decoder, &_si)) {
warning("Cannot initialize musepack decoder");
return;
}
#else
_demux = mpc_demux_init(&_reader);
if (!_demux) {
warning("Cannot init musepack demuxer");
return;
}
mpc_demux_get_info(_demux, &_si);
#endif
_isStereo = _si.channels >= 2;
_rate = _si.sample_freq;
_length = Timestamp(uint32(mpc_streaminfo_get_length(&_si) * 1000.0), getRate());
int time = (int)mpc_streaminfo_get_length(&_si);
int minutes = time / 60;
int seconds = time % 60;
debug(9, "stream version %d", _si.stream_version);
debug(9, "encoder: %s", _si.encoder);
#ifdef USE_MPCDEC_OLD_API
debug(9, "profile: %s (q=%d)", _si.profile_name, _si.profile);
#else
debug(9, "profile: %s (q=%0.2f)", _si.profile_name, _si.profile - 5);
debug(9, "PNS: %s", _si.pns == 0xFF ? "unknow" : _si.pns ? "on" : "off");
#endif
debug(9, "mid/side stereo: %s", _si.ms ? "on" : "off");
debug(9, "gapless: %s", _si.is_true_gapless ? "on" : "off");
debug(9, "average bitrate: %6.1f kbps", _si.average_bitrate * 1.e-3);
debug(9, "samplerate: %d Hz", _si.sample_freq);
debug(9, "channels: %d", _si.channels);
debug(9, "length: %d:%.2d (%u samples)", minutes, seconds, (mpc_uint32_t)mpc_streaminfo_get_length_samples(&_si));
debug(9, "file size: %d Bytes", _si.total_file_length);
debug(9, "track peak: %2.2f dB", _si.peak_title / 256.f);
debug(9, "track gain: %2.2f dB / %2.2f dB", _si.gain_title / 256.f, _si.gain_title == 0 ? 0 : 64.82f - _si.gain_title / 256.f);
debug(9, "album peak: %2.2f dB", _si.peak_album / 256.f);
debug(9, "album gain: %2.2f dB / %2.2f dB", _si.gain_album / 256.f, _si.gain_album == 0 ? 0 : 64.82f - _si.gain_album / 256.f);
if (!refill())
return;
}
MPCStream::~MPCStream() {
#ifndef USE_MPCDEC_OLD_API
mpc_demux_exit(_demux);
#endif
}
int MPCStream::readBuffer(int16 *buffer, const int numSamples) {
int samples = 0;
while (samples < numSamples && _pos < _bufferEnd) {
const int len = MIN(numSamples - samples, (int)(_bufferEnd - _pos));
memcpy(buffer, _pos, len * 2);
buffer += len;
_pos += len;
samples += len;
if (_pos >= _bufferEnd) {
if (!refill())
break;
}
}
return samples;
}
bool MPCStream::seek(const Timestamp &where) {
#ifdef USE_MPCDEC_OLD_API
bool res = (mpc_decoder_seek_seconds(&_decoder, (double)where.msecs() / 1000.0) == TRUE);
#else
bool res = (mpc_demux_seek_second(_demux, (double)where.msecs() / 1000.0) == MPC_STATUS_OK);
#endif
if (!res) {
warning("Error seeking in musepack stream");
_pos = _bufferEnd;
return false;
}
return refill();
}
bool MPCStream::refill() {
bool result;
uint32 samples;
#ifdef USE_MPCDEC_OLD_API
uint32 vbr_update_acc, vbr_update_bits;
samples = mpc_decoder_decode(&_decoder, _bufferDec, &vbr_update_acc, &vbr_update_bits);
if (samples == 0) { // End of stream
_pos = _buffer;
_bufferEnd = _buffer;
return false;
}
if (samples == -1u) { // Corruptd stream
result = false;
samples = 0;
} else {
result = true;
}
#else
mpc_frame_info frame;
frame.buffer = _bufferDec;
result = (mpc_demux_decode(_demux, &frame) == MPC_STATUS_OK);
if (frame.bits == -1) { // End of stream
_pos = _buffer;
_bufferEnd = _buffer;
return false;
}
samples = frame.samples;
#endif
if (!result) {
// Possibly recoverable, just warn about it
warning("Corrupted data in musepack file");
}
#ifdef MPC_FIXED_POINT
for(int i = 0; i < MPC_DECODER_BUFFER_LENGTH; i++) {
int tmp = _bufferDec[i] >> MPC_FIXED_POINT_FRACTPART;
if (tmp > ((1 << 15) - 1)) tmp = ((1 << 15) - 1);
if (tmp < -(1 << 15)) tmp = -(1 << 15);
_buffer[i] = tmp;
}
#else
for (int i = 0; i < MPC_DECODER_BUFFER_LENGTH; i++) {
int tmp = nearbyintf(_bufferDec[i] * (1 << 15));
if (tmp > ((1 << 15) - 1))
tmp = ((1 << 15) - 1);
if (tmp < -(1 << 15))
tmp = -(1 << 15);
_buffer[i] = (uint16)tmp;
}
#endif
_pos = _buffer;
_bufferEnd = &_buffer[samples * _si.channels];
return true;
}
#pragma mark -
#pragma mark --- Ogg Vorbis factory functions ---
#pragma mark -
SeekableAudioStream *makeMPCStream(
Common::SeekableReadStream *stream,
DisposeAfterUse::Flag disposeAfterUse) {
SeekableAudioStream *s = new MPCStream(stream, disposeAfterUse);
if (s && s->endOfData()) {
delete s;
return nullptr;
} else {
return s;
}
}
} // End of namespace Audio
#endif // #ifdef USE_MPCDEC
|