From c44c25561c9f16897f51ff41a485cc4e7099f088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrian=20Fr=C3=BChwirth?= Date: Thu, 7 Jun 2018 01:01:55 +0200 Subject: [PATCH] TOOLS: Fix internal FLAC encoder This commit fixes the internal FLAC encoder to properly handle input of different endianness which means compressing audio with FLAC should now work as expected on all supported platforms. Up until now the encoder only worked with signed 16-bit PCM in native endianess which worked in most cases because most games using 16-bit audio use pcm_s16le and scummvm-tools are usually run on LE x86/x86_64 hardware. Two cases of s16be audio are the recently unlocked (see commit 72655d8) speech sample in Indiana Jones and the Fate of Atlantis as well as certain Mac versions of Broken Sword 1. --- compress.cpp | 18 +++++++++++------- engines/scumm/compress_scumm_sou.cpp | 1 - 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/compress.cpp b/compress.cpp index f62e6e0d..218acd3f 100644 --- a/compress.cpp +++ b/compress.cpp @@ -310,7 +310,6 @@ void CompressionTool::encodeAudio(const char *inname, bool rawInput, int rawSamp } void CompressionTool::encodeRaw(const char *rawData, int length, int samplerate, const char *outname, AudioFormat compmode) { - print(" - len=%ld, ch=%d, rate=%d, %dbits", length, (rawAudioType.isStereo ? 2 : 1), samplerate, rawAudioType.bitsPerSample); #ifdef USE_VORBIS @@ -529,14 +528,19 @@ void CompressionTool::encodeRaw(const char *rawData, int length, int samplerate, if (rawAudioType.bitsPerSample == 8) { for (i = 0; i < samplesPerChannel * numChannels; i++) { - const FLAC__uint8 *rawDataUnsigned = (const FLAC__uint8 *)rawData; - flacData[i] = (FLAC__int32)rawDataUnsigned[i] - 0x80; + flacData[i] = (FLAC__int32)(FLAC__uint8)rawData[i] - 0x80; } } else if (rawAudioType.bitsPerSample == 16) { - /* The rawData pointer is an 8-bit char so we must create a new pointer to access 16-bit samples */ - const FLAC__int16 *rawData16 = (const FLAC__int16 *)rawData; - for (i = 0; i < samplesPerChannel * numChannels; i++) { - flacData[i] = (FLAC__int32)rawData16[i]; + if (rawAudioType.isLittleEndian) { + for (i = 0; i < samplesPerChannel * numChannels; i++) { + flacData[i] = (FLAC__int32)((FLAC__int16)(FLAC__int8)(FLAC__byte)rawData[2 * i + 1] << 8 | + (FLAC__int16)(FLAC__byte)rawData[2 * i ]); + } + } else { + for (i = 0; i < samplesPerChannel * numChannels; i++) { + flacData[i] = (FLAC__int32)((FLAC__int16)(FLAC__int8)(FLAC__byte)rawData[2 * i ] << 8 | + (FLAC__int16)(FLAC__byte)rawData[2 * i + 1]); + } } } diff --git a/engines/scumm/compress_scumm_sou.cpp b/engines/scumm/compress_scumm_sou.cpp index 70cf6476..815288a1 100644 --- a/engines/scumm/compress_scumm_sou.cpp +++ b/engines/scumm/compress_scumm_sou.cpp @@ -178,7 +178,6 @@ bool CompressScummSou::get_part() { _output_idx.writeUint32BE(tot_size); updateProgress(_input.pos(), _file_size); - return true; }