1From 7d31da8ef93987000f297d435dbacaf7d436107b Mon Sep 17 00:00:00 2001
2From: Tanu Kaskinen <tanuk@iki.fi>
3Date: Thu, 15 Jun 2017 18:38:30 +0300
4Subject: [PATCH 6/6] common_audio: implement endianness conversion in wav file
5 handling
6
7The code didn't build for big endian machines due to the missing
8endianness conversions.
9
10Upstream-Status: Pending
11Signed-off-by: Tanu Kaskinen <tanuk@iki.fi>
12---
13 webrtc/common_audio/wav_file.cc   | 27 +++++++++++++++++++++------
14 webrtc/common_audio/wav_header.cc | 32 +++++++++++++++++++++++++++++++-
15 2 files changed, 52 insertions(+), 7 deletions(-)
16
17diff --git a/webrtc/common_audio/wav_file.cc b/webrtc/common_audio/wav_file.cc
18index b14b620..e2f7738 100644
19--- a/webrtc/common_audio/wav_file.cc
20+++ b/webrtc/common_audio/wav_file.cc
21@@ -64,9 +64,6 @@ WavReader::~WavReader() {
22 }
23
24 size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) {
25-#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
26-#error "Need to convert samples to big-endian when reading from WAV file"
27-#endif
28   // There could be metadata after the audio; ensure we don't read it.
29   num_samples = std::min(rtc::checked_cast<uint32_t>(num_samples),
30                          num_samples_remaining_);
31@@ -76,6 +73,13 @@ size_t WavReader::ReadSamples(size_t num_samples, int16_t* samples) {
32   RTC_CHECK(read == num_samples || feof(file_handle_));
33   RTC_CHECK_LE(read, num_samples_remaining_);
34   num_samples_remaining_ -= rtc::checked_cast<uint32_t>(read);
35+
36+#ifdef WEBRTC_ARCH_BIG_ENDIAN
37+  // Convert the read samples from little-endian to big-endian.
38+  for (size_t i = 0; i < read; i++)
39+    samples[i] = ((uint16_t) samples[i] >> 8) | ((uint16_t) samples[i] << 8);
40+#endif
41+
42   return read;
43 }
44
45@@ -119,11 +123,22 @@ WavWriter::~WavWriter() {
46 }
47
48 void WavWriter::WriteSamples(const int16_t* samples, size_t num_samples) {
49-#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
50-#error "Need to convert samples to little-endian when writing to WAV file"
51-#endif
52+#ifdef WEBRTC_ARCH_BIG_ENDIAN
53+  // Convert the samples from big-endian samples to little-endian.
54+  int16_t* converted_samples = static_cast<int16_t*>(malloc(num_samples * sizeof(*samples)));
55+  RTC_CHECK(converted_samples) << "Out of memory.";
56+  for (int i = 0; i < num_samples; i++)
57+    converted_samples[i] =
58+        ((uint16_t) samples[i] >> 8) | ((uint16_t) samples[i] << 8);
59+
60+  const size_t written =
61+      fwrite(converted_samples, sizeof(*converted_samples), num_samples,
62+             file_handle_);
63+  free(converted_samples);
64+#else
65   const size_t written =
66       fwrite(samples, sizeof(*samples), num_samples, file_handle_);
67+#endif
68   RTC_CHECK_EQ(num_samples, written);
69   num_samples_ += static_cast<uint32_t>(written);
70   RTC_CHECK(written <= std::numeric_limits<uint32_t>::max() ||
71diff --git a/webrtc/common_audio/wav_header.cc b/webrtc/common_audio/wav_header.cc
72index 61cfffe..382bfc7 100644
73--- a/webrtc/common_audio/wav_header.cc
74+++ b/webrtc/common_audio/wav_header.cc
75@@ -129,7 +129,37 @@ static inline std::string ReadFourCC(uint32_t x) {
76   return std::string(reinterpret_cast<char*>(&x), 4);
77 }
78 #else
79-#error "Write be-to-le conversion functions"
80+static inline void WriteLE16(uint16_t* f, uint16_t x) {
81+  *f = x >> 8 | x << 8;
82+}
83+static inline void WriteLE32(uint32_t* f, uint32_t x) {
84+  *f = x >> 24
85+      | (x && 0xFF0000) >> 8
86+      | (x && 0xFF00) << 8
87+      | x << 24;
88+}
89+static inline void WriteFourCC(uint32_t* f, char a, char b, char c, char d) {
90+  *f = static_cast<uint32_t>(d)
91+      | static_cast<uint32_t>(c) << 8
92+      | static_cast<uint32_t>(b) << 16
93+      | static_cast<uint32_t>(a) << 24;
94+}
95+
96+static inline uint32_t ReadLE16(uint16_t x) {
97+  return x >> 8 | x << 8;
98+}
99+
100+static inline uint32_t ReadLE32(uint32_t x) {
101+  return x >> 24
102+      | (x && 0xFF0000) >> 8
103+      | (x && 0xFF00) << 8
104+      | x << 24;
105+}
106+
107+static inline std::string ReadFourCC(uint32_t x) {
108+  x = ReadLE32(x);
109+  return std::string(reinterpret_cast<char*>(&x), 4);
110+}
111 #endif
112
113 static inline uint32_t RiffChunkSize(uint32_t bytes_in_payload) {
114--
1152.11.0
116
117