Fixed build of libtgvoip on Big-Endian architectures.

This commit is contained in:
Vitaly Zaitsev 2018-02-20 14:00:02 +01:00 committed by Alex
parent e1a947e850
commit 356f1cb083
3 changed files with 61 additions and 7 deletions

View File

@ -77,9 +77,6 @@ size_t WavReader::num_samples() const {
} }
size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) { size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) {
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
#error "Need to convert samples to big-endian when reading from WAV file"
#endif
// There could be metadata after the audio; ensure we don't read it. // There could be metadata after the audio; ensure we don't read it.
num_samples = std::min(num_samples, num_samples_remaining_); num_samples = std::min(num_samples, num_samples_remaining_);
const size_t read = const size_t read =
@ -88,6 +85,12 @@ size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) {
RTC_CHECK(read == num_samples || feof(file_handle_)); RTC_CHECK(read == num_samples || feof(file_handle_));
RTC_CHECK_LE(read, num_samples_remaining_); RTC_CHECK_LE(read, num_samples_remaining_);
num_samples_remaining_ -= read; num_samples_remaining_ -= read;
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
//convert to big-endian
for(size_t idx = 0; idx < num_samples; idx++) {
samples[idx] = (samples[idx]<<8) | (samples[idx]>>8);
}
#endif
return read; return read;
} }
@ -144,10 +147,17 @@ size_t WavWriter::num_samples() const {
void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) { void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) {
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN #ifndef WEBRTC_ARCH_LITTLE_ENDIAN
#error "Need to convert samples to little-endian when writing to WAV file" int16_t * le_samples = new int16_t[num_samples];
#endif for(size_t idx = 0; idx < num_samples; idx++) {
le_samples[idx] = (samples[idx]<<8) | (samples[idx]>>8);
}
const size_t written =
fwrite(le_samples, sizeof(*le_samples), num_samples, file_handle_);
delete []le_samples;
#else
const size_t written = const size_t written =
fwrite(samples, sizeof(*samples), num_samples, file_handle_); fwrite(samples, sizeof(*samples), num_samples, file_handle_);
#endif
RTC_CHECK_EQ(num_samples, written); RTC_CHECK_EQ(num_samples, written);
num_samples_ += written; num_samples_ += written;
RTC_CHECK(num_samples_ >= written); // detect size_t overflow RTC_CHECK(num_samples_ >= written); // detect size_t overflow

View File

@ -127,7 +127,39 @@ static inline std::string ReadFourCC(uint32_t x) {
return std::string(reinterpret_cast<char*>(&x), 4); return std::string(reinterpret_cast<char*>(&x), 4);
} }
#else #else
#error "Write be-to-le conversion functions" static inline void WriteLE16(uint16_t* f, uint16_t x) {
*f = ((x << 8) & 0xff00) | ( ( x >> 8) & 0x00ff);
}
static inline void WriteLE32(uint32_t* f, uint32_t x) {
*f = ( (x & 0x000000ff) << 24 )
| ((x & 0x0000ff00) << 8)
| ((x & 0x00ff0000) >> 8)
| ((x & 0xff000000) >> 24 );
}
static inline void WriteFourCC(uint32_t* f, char a, char b, char c, char d) {
*f = (static_cast<uint32_t>(a) << 24 )
| (static_cast<uint32_t>(b) << 16)
| (static_cast<uint32_t>(c) << 8)
| (static_cast<uint32_t>(d) );
}
static inline uint16_t ReadLE16(uint16_t x) {
return (( x & 0x00ff) << 8 )| ((x & 0xff00)>>8);
}
static inline uint32_t ReadLE32(uint32_t x) {
return ( (x & 0x000000ff) << 24 )
| ( (x & 0x0000ff00) << 8 )
| ( (x & 0x00ff0000) >> 8)
| ( (x & 0xff000000) >> 24 );
}
static inline std::string ReadFourCC(uint32_t x) {
x = ReadLE32(x);
return std::string(reinterpret_cast<char*>(&x), 4);
}
#endif #endif
static inline uint32_t RiffChunkSize(size_t bytes_in_payload) { static inline uint32_t RiffChunkSize(size_t bytes_in_payload) {

View File

@ -48,7 +48,19 @@
#define WEBRTC_ARCH_32_BITS #define WEBRTC_ARCH_32_BITS
#define WEBRTC_ARCH_LITTLE_ENDIAN #define WEBRTC_ARCH_LITTLE_ENDIAN
#else #else
#error Please add support for your architecture in typedefs.h /* instead of failing, use typical unix defines... */
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define WEBRTC_ARCH_LITTLE_ENDIAN
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define WEBRTC_ARCH_BIG_ENDIAN
#else
#error __BYTE_ORDER__ is not defined
#endif
#if defined(__LP64__)
#define WEBRTC_ARCH_64_BITS
#else
#define WEBRTC_ARCH_32_BITS
#endif
#endif #endif
#if !(defined(WEBRTC_ARCH_LITTLE_ENDIAN) ^ defined(WEBRTC_ARCH_BIG_ENDIAN)) #if !(defined(WEBRTC_ARCH_LITTLE_ENDIAN) ^ defined(WEBRTC_ARCH_BIG_ENDIAN))