kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | // Based on the WAV file format documentation at |
| 12 | // https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ and |
| 13 | // http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html |
| 14 | |
| 15 | #include "webrtc/common_audio/wav_header.h" |
| 16 | |
| 17 | #include <algorithm> |
| 18 | #include <cstring> |
| 19 | #include <limits> |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 20 | #include <string> |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 21 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 22 | #include "webrtc/base/checks.h" |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 23 | #include "webrtc/common_audio/include/audio_util.h" |
| 24 | |
| 25 | namespace webrtc { |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 26 | namespace { |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 27 | |
| 28 | struct ChunkHeader { |
| 29 | uint32_t ID; |
| 30 | uint32_t Size; |
| 31 | }; |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 32 | static_assert(sizeof(ChunkHeader) == 8, "ChunkHeader size"); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 33 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 34 | // We can't nest this definition in WavHeader, because VS2013 gives an error |
| 35 | // on sizeof(WavHeader::fmt): "error C2070: 'unknown': illegal sizeof operand". |
| 36 | struct FmtSubchunk { |
| 37 | ChunkHeader header; |
| 38 | uint16_t AudioFormat; |
| 39 | uint16_t NumChannels; |
| 40 | uint32_t SampleRate; |
| 41 | uint32_t ByteRate; |
| 42 | uint16_t BlockAlign; |
| 43 | uint16_t BitsPerSample; |
| 44 | }; |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 45 | static_assert(sizeof(FmtSubchunk) == 24, "FmtSubchunk size"); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 46 | const uint32_t kFmtSubchunkSize = sizeof(FmtSubchunk) - sizeof(ChunkHeader); |
| 47 | |
| 48 | struct WavHeader { |
| 49 | struct { |
| 50 | ChunkHeader header; |
| 51 | uint32_t Format; |
| 52 | } riff; |
| 53 | FmtSubchunk fmt; |
| 54 | struct { |
| 55 | ChunkHeader header; |
| 56 | } data; |
| 57 | }; |
kwiberg@webrtc.org | 2ebfac5 | 2015-01-14 10:51:54 +0000 | [diff] [blame] | 58 | static_assert(sizeof(WavHeader) == kWavHeaderSize, "no padding in header"); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 59 | |
| 60 | } // namespace |
| 61 | |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 62 | bool CheckWavParameters(int num_channels, |
| 63 | int sample_rate, |
| 64 | WavFormat format, |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame^] | 65 | size_t bytes_per_sample, |
| 66 | size_t num_samples) { |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 67 | // num_channels, sample_rate, and bytes_per_sample must be positive, must fit |
| 68 | // in their respective fields, and their product must fit in the 32-bit |
| 69 | // ByteRate field. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame^] | 70 | if (num_channels <= 0 || sample_rate <= 0 || bytes_per_sample == 0) |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 71 | return false; |
| 72 | if (static_cast<uint64_t>(sample_rate) > std::numeric_limits<uint32_t>::max()) |
| 73 | return false; |
| 74 | if (static_cast<uint64_t>(num_channels) > |
| 75 | std::numeric_limits<uint16_t>::max()) |
| 76 | return false; |
| 77 | if (static_cast<uint64_t>(bytes_per_sample) * 8 > |
| 78 | std::numeric_limits<uint16_t>::max()) |
| 79 | return false; |
| 80 | if (static_cast<uint64_t>(sample_rate) * num_channels * bytes_per_sample > |
| 81 | std::numeric_limits<uint32_t>::max()) |
| 82 | return false; |
| 83 | |
| 84 | // format and bytes_per_sample must agree. |
| 85 | switch (format) { |
| 86 | case kWavFormatPcm: |
| 87 | // Other values may be OK, but for now we're conservative: |
| 88 | if (bytes_per_sample != 1 && bytes_per_sample != 2) |
| 89 | return false; |
| 90 | break; |
| 91 | case kWavFormatALaw: |
| 92 | case kWavFormatMuLaw: |
| 93 | if (bytes_per_sample != 1) |
| 94 | return false; |
| 95 | break; |
| 96 | default: |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | // The number of bytes in the file, not counting the first ChunkHeader, must |
| 101 | // be less than 2^32; otherwise, the ChunkSize field overflows. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame^] | 102 | const size_t header_size = kWavHeaderSize - sizeof(ChunkHeader); |
| 103 | const size_t max_samples = |
| 104 | (std::numeric_limits<uint32_t>::max() - header_size) / bytes_per_sample; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 105 | if (num_samples > max_samples) |
| 106 | return false; |
| 107 | |
| 108 | // Each channel must have the same number of samples. |
| 109 | if (num_samples % num_channels != 0) |
| 110 | return false; |
| 111 | |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | #ifdef WEBRTC_ARCH_LITTLE_ENDIAN |
| 116 | static inline void WriteLE16(uint16_t* f, uint16_t x) { *f = x; } |
| 117 | static inline void WriteLE32(uint32_t* f, uint32_t x) { *f = x; } |
| 118 | static inline void WriteFourCC(uint32_t* f, char a, char b, char c, char d) { |
| 119 | *f = static_cast<uint32_t>(a) |
| 120 | | static_cast<uint32_t>(b) << 8 |
| 121 | | static_cast<uint32_t>(c) << 16 |
| 122 | | static_cast<uint32_t>(d) << 24; |
| 123 | } |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 124 | |
| 125 | static inline uint16_t ReadLE16(uint16_t x) { return x; } |
| 126 | static inline uint32_t ReadLE32(uint32_t x) { return x; } |
| 127 | static inline std::string ReadFourCC(uint32_t x) { |
| 128 | return std::string(reinterpret_cast<char*>(&x), 4); |
| 129 | } |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 130 | #else |
| 131 | #error "Write be-to-le conversion functions" |
| 132 | #endif |
| 133 | |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame^] | 134 | static inline uint32_t RiffChunkSize(size_t bytes_in_payload) { |
| 135 | return static_cast<uint32_t>( |
| 136 | bytes_in_payload + kWavHeaderSize - sizeof(ChunkHeader)); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static inline uint32_t ByteRate(int num_channels, int sample_rate, |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame^] | 140 | size_t bytes_per_sample) { |
| 141 | return static_cast<uint32_t>(num_channels * sample_rate * bytes_per_sample); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 142 | } |
| 143 | |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame^] | 144 | static inline uint16_t BlockAlign(int num_channels, size_t bytes_per_sample) { |
| 145 | return static_cast<uint16_t>(num_channels * bytes_per_sample); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 146 | } |
| 147 | |
andrew@webrtc.org | f866b2d | 2014-11-03 18:20:06 +0000 | [diff] [blame] | 148 | void WriteWavHeader(uint8_t* buf, |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 149 | int num_channels, |
| 150 | int sample_rate, |
| 151 | WavFormat format, |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame^] | 152 | size_t bytes_per_sample, |
| 153 | size_t num_samples) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 154 | RTC_CHECK(CheckWavParameters(num_channels, sample_rate, format, |
| 155 | bytes_per_sample, num_samples)); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 156 | |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 157 | WavHeader header; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame^] | 158 | const size_t bytes_in_payload = bytes_per_sample * num_samples; |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 159 | |
| 160 | WriteFourCC(&header.riff.header.ID, 'R', 'I', 'F', 'F'); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 161 | WriteLE32(&header.riff.header.Size, RiffChunkSize(bytes_in_payload)); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 162 | WriteFourCC(&header.riff.Format, 'W', 'A', 'V', 'E'); |
| 163 | |
| 164 | WriteFourCC(&header.fmt.header.ID, 'f', 'm', 't', ' '); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 165 | WriteLE32(&header.fmt.header.Size, kFmtSubchunkSize); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 166 | WriteLE16(&header.fmt.AudioFormat, format); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame^] | 167 | WriteLE16(&header.fmt.NumChannels, static_cast<uint16_t>(num_channels)); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 168 | WriteLE32(&header.fmt.SampleRate, sample_rate); |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 169 | WriteLE32(&header.fmt.ByteRate, ByteRate(num_channels, sample_rate, |
| 170 | bytes_per_sample)); |
| 171 | WriteLE16(&header.fmt.BlockAlign, BlockAlign(num_channels, bytes_per_sample)); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame^] | 172 | WriteLE16(&header.fmt.BitsPerSample, |
| 173 | static_cast<uint16_t>(8 * bytes_per_sample)); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 174 | |
| 175 | WriteFourCC(&header.data.header.ID, 'd', 'a', 't', 'a'); |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame^] | 176 | WriteLE32(&header.data.header.Size, static_cast<uint32_t>(bytes_in_payload)); |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 177 | |
| 178 | // Do an extra copy rather than writing everything to buf directly, since buf |
| 179 | // might not be correctly aligned. |
| 180 | memcpy(buf, &header, kWavHeaderSize); |
| 181 | } |
| 182 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 183 | bool ReadWavHeader(ReadableWav* readable, |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 184 | int* num_channels, |
| 185 | int* sample_rate, |
| 186 | WavFormat* format, |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame^] | 187 | size_t* bytes_per_sample, |
| 188 | size_t* num_samples) { |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 189 | WavHeader header; |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 190 | if (readable->Read(&header, kWavHeaderSize - sizeof(header.data)) != |
| 191 | kWavHeaderSize - sizeof(header.data)) |
| 192 | return false; |
| 193 | |
| 194 | const uint32_t fmt_size = ReadLE32(header.fmt.header.Size); |
| 195 | if (fmt_size != kFmtSubchunkSize) { |
| 196 | // There is an optional two-byte extension field permitted to be present |
| 197 | // with PCM, but which must be zero. |
| 198 | int16_t ext_size; |
| 199 | if (kFmtSubchunkSize + sizeof(ext_size) != fmt_size) |
| 200 | return false; |
| 201 | if (readable->Read(&ext_size, sizeof(ext_size)) != sizeof(ext_size)) |
| 202 | return false; |
| 203 | if (ext_size != 0) |
| 204 | return false; |
| 205 | } |
| 206 | if (readable->Read(&header.data, sizeof(header.data)) != sizeof(header.data)) |
| 207 | return false; |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 208 | |
| 209 | // Parse needed fields. |
| 210 | *format = static_cast<WavFormat>(ReadLE16(header.fmt.AudioFormat)); |
| 211 | *num_channels = ReadLE16(header.fmt.NumChannels); |
| 212 | *sample_rate = ReadLE32(header.fmt.SampleRate); |
| 213 | *bytes_per_sample = ReadLE16(header.fmt.BitsPerSample) / 8; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame^] | 214 | const size_t bytes_in_payload = ReadLE32(header.data.header.Size); |
| 215 | if (*bytes_per_sample == 0) |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 216 | return false; |
| 217 | *num_samples = bytes_in_payload / *bytes_per_sample; |
| 218 | |
| 219 | // Sanity check remaining fields. |
| 220 | if (ReadFourCC(header.riff.header.ID) != "RIFF") |
| 221 | return false; |
| 222 | if (ReadFourCC(header.riff.Format) != "WAVE") |
| 223 | return false; |
| 224 | if (ReadFourCC(header.fmt.header.ID) != "fmt ") |
| 225 | return false; |
| 226 | if (ReadFourCC(header.data.header.ID) != "data") |
| 227 | return false; |
| 228 | |
andrew@webrtc.org | 048c502 | 2014-12-16 20:17:21 +0000 | [diff] [blame] | 229 | if (ReadLE32(header.riff.header.Size) < RiffChunkSize(bytes_in_payload)) |
andrew@webrtc.org | a3ed713 | 2014-10-31 21:51:03 +0000 | [diff] [blame] | 230 | return false; |
| 231 | if (ReadLE32(header.fmt.ByteRate) != |
| 232 | ByteRate(*num_channels, *sample_rate, *bytes_per_sample)) |
| 233 | return false; |
| 234 | if (ReadLE16(header.fmt.BlockAlign) != |
| 235 | BlockAlign(*num_channels, *bytes_per_sample)) |
| 236 | return false; |
| 237 | |
| 238 | return CheckWavParameters(*num_channels, *sample_rate, *format, |
| 239 | *bytes_per_sample, *num_samples); |
| 240 | } |
| 241 | |
| 242 | |
kwiberg@webrtc.org | 877083c | 2014-08-20 07:42:46 +0000 | [diff] [blame] | 243 | } // namespace webrtc |