blob: 8fc5fef5085230668379dec0f29476430552c673 [file] [log] [blame]
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +00001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "common_audio/wav_header.h"
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000016
17#include <algorithm>
18#include <cstring>
19#include <limits>
andrew@webrtc.org048c5022014-12-16 20:17:21 +000020#include <string>
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000021
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "common_audio/include/audio_util.h"
23#include "rtc_base/checks.h"
Niels Möllera12c42a2018-07-25 16:05:48 +020024#include "rtc_base/system/arch.h"
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000025
26namespace webrtc {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000027namespace {
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000028
29struct ChunkHeader {
30 uint32_t ID;
31 uint32_t Size;
32};
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +000033static_assert(sizeof(ChunkHeader) == 8, "ChunkHeader size");
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000034
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000035// We can't nest this definition in WavHeader, because VS2013 gives an error
36// on sizeof(WavHeader::fmt): "error C2070: 'unknown': illegal sizeof operand".
37struct FmtSubchunk {
38 ChunkHeader header;
39 uint16_t AudioFormat;
40 uint16_t NumChannels;
41 uint32_t SampleRate;
42 uint32_t ByteRate;
43 uint16_t BlockAlign;
44 uint16_t BitsPerSample;
45};
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +000046static_assert(sizeof(FmtSubchunk) == 24, "FmtSubchunk size");
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000047const uint32_t kFmtSubchunkSize = sizeof(FmtSubchunk) - sizeof(ChunkHeader);
48
49struct WavHeader {
50 struct {
51 ChunkHeader header;
52 uint32_t Format;
53 } riff;
54 FmtSubchunk fmt;
55 struct {
56 ChunkHeader header;
57 } data;
58};
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +000059static_assert(sizeof(WavHeader) == kWavHeaderSize, "no padding in header");
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000060
61} // namespace
62
Peter Kasting69558702016-01-12 16:26:35 -080063bool CheckWavParameters(size_t num_channels,
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000064 int sample_rate,
65 WavFormat format,
pkasting25702cb2016-01-08 13:50:27 -080066 size_t bytes_per_sample,
67 size_t num_samples) {
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000068 // num_channels, sample_rate, and bytes_per_sample must be positive, must fit
69 // in their respective fields, and their product must fit in the 32-bit
70 // ByteRate field.
Peter Kasting69558702016-01-12 16:26:35 -080071 if (num_channels == 0 || sample_rate <= 0 || bytes_per_sample == 0)
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000072 return false;
73 if (static_cast<uint64_t>(sample_rate) > std::numeric_limits<uint32_t>::max())
74 return false;
Peter Kasting69558702016-01-12 16:26:35 -080075 if (num_channels > std::numeric_limits<uint16_t>::max())
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000076 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.
pkasting25702cb2016-01-08 13:50:27 -0800102 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.org877083c2014-08-20 07:42:46 +0000105 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
Yves Gerey665174f2018-06-19 15:03:05 +0200116static inline void WriteLE16(uint16_t* f, uint16_t x) {
117 *f = x;
118}
119static inline void WriteLE32(uint32_t* f, uint32_t x) {
120 *f = x;
121}
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000122static inline void WriteFourCC(uint32_t* f, char a, char b, char c, char d) {
Yves Gerey665174f2018-06-19 15:03:05 +0200123 *f = static_cast<uint32_t>(a) | static_cast<uint32_t>(b) << 8 |
124 static_cast<uint32_t>(c) << 16 | static_cast<uint32_t>(d) << 24;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000125}
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000126
Yves Gerey665174f2018-06-19 15:03:05 +0200127static inline uint16_t ReadLE16(uint16_t x) {
128 return x;
129}
130static inline uint32_t ReadLE32(uint32_t x) {
131 return x;
132}
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000133static inline std::string ReadFourCC(uint32_t x) {
134 return std::string(reinterpret_cast<char*>(&x), 4);
135}
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000136#else
137#error "Write be-to-le conversion functions"
138#endif
139
pkasting25702cb2016-01-08 13:50:27 -0800140static inline uint32_t RiffChunkSize(size_t bytes_in_payload) {
Yves Gerey665174f2018-06-19 15:03:05 +0200141 return static_cast<uint32_t>(bytes_in_payload + kWavHeaderSize -
142 sizeof(ChunkHeader));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000143}
144
Yves Gerey665174f2018-06-19 15:03:05 +0200145static inline uint32_t ByteRate(size_t num_channels,
146 int sample_rate,
pkasting25702cb2016-01-08 13:50:27 -0800147 size_t bytes_per_sample) {
148 return static_cast<uint32_t>(num_channels * sample_rate * bytes_per_sample);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000149}
150
Peter Kasting69558702016-01-12 16:26:35 -0800151static inline uint16_t BlockAlign(size_t num_channels,
152 size_t bytes_per_sample) {
pkasting25702cb2016-01-08 13:50:27 -0800153 return static_cast<uint16_t>(num_channels * bytes_per_sample);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000154}
155
andrew@webrtc.orgf866b2d2014-11-03 18:20:06 +0000156void WriteWavHeader(uint8_t* buf,
Peter Kasting69558702016-01-12 16:26:35 -0800157 size_t num_channels,
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000158 int sample_rate,
159 WavFormat format,
pkasting25702cb2016-01-08 13:50:27 -0800160 size_t bytes_per_sample,
161 size_t num_samples) {
henrikg91d6ede2015-09-17 00:24:34 -0700162 RTC_CHECK(CheckWavParameters(num_channels, sample_rate, format,
163 bytes_per_sample, num_samples));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000164
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000165 WavHeader header;
pkasting25702cb2016-01-08 13:50:27 -0800166 const size_t bytes_in_payload = bytes_per_sample * num_samples;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000167
168 WriteFourCC(&header.riff.header.ID, 'R', 'I', 'F', 'F');
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000169 WriteLE32(&header.riff.header.Size, RiffChunkSize(bytes_in_payload));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000170 WriteFourCC(&header.riff.Format, 'W', 'A', 'V', 'E');
171
172 WriteFourCC(&header.fmt.header.ID, 'f', 'm', 't', ' ');
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000173 WriteLE32(&header.fmt.header.Size, kFmtSubchunkSize);
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000174 WriteLE16(&header.fmt.AudioFormat, format);
pkasting25702cb2016-01-08 13:50:27 -0800175 WriteLE16(&header.fmt.NumChannels, static_cast<uint16_t>(num_channels));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000176 WriteLE32(&header.fmt.SampleRate, sample_rate);
Yves Gerey665174f2018-06-19 15:03:05 +0200177 WriteLE32(&header.fmt.ByteRate,
178 ByteRate(num_channels, sample_rate, bytes_per_sample));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000179 WriteLE16(&header.fmt.BlockAlign, BlockAlign(num_channels, bytes_per_sample));
pkasting25702cb2016-01-08 13:50:27 -0800180 WriteLE16(&header.fmt.BitsPerSample,
181 static_cast<uint16_t>(8 * bytes_per_sample));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000182
183 WriteFourCC(&header.data.header.ID, 'd', 'a', 't', 'a');
pkasting25702cb2016-01-08 13:50:27 -0800184 WriteLE32(&header.data.header.Size, static_cast<uint32_t>(bytes_in_payload));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000185
186 // Do an extra copy rather than writing everything to buf directly, since buf
187 // might not be correctly aligned.
188 memcpy(buf, &header, kWavHeaderSize);
189}
190
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000191bool ReadWavHeader(ReadableWav* readable,
Peter Kasting69558702016-01-12 16:26:35 -0800192 size_t* num_channels,
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000193 int* sample_rate,
194 WavFormat* format,
pkasting25702cb2016-01-08 13:50:27 -0800195 size_t* bytes_per_sample,
196 size_t* num_samples) {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000197 WavHeader header;
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000198 if (readable->Read(&header, kWavHeaderSize - sizeof(header.data)) !=
199 kWavHeaderSize - sizeof(header.data))
200 return false;
201
202 const uint32_t fmt_size = ReadLE32(header.fmt.header.Size);
203 if (fmt_size != kFmtSubchunkSize) {
204 // There is an optional two-byte extension field permitted to be present
205 // with PCM, but which must be zero.
206 int16_t ext_size;
207 if (kFmtSubchunkSize + sizeof(ext_size) != fmt_size)
208 return false;
209 if (readable->Read(&ext_size, sizeof(ext_size)) != sizeof(ext_size))
210 return false;
211 if (ext_size != 0)
212 return false;
213 }
214 if (readable->Read(&header.data, sizeof(header.data)) != sizeof(header.data))
215 return false;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000216
217 // Parse needed fields.
218 *format = static_cast<WavFormat>(ReadLE16(header.fmt.AudioFormat));
219 *num_channels = ReadLE16(header.fmt.NumChannels);
220 *sample_rate = ReadLE32(header.fmt.SampleRate);
221 *bytes_per_sample = ReadLE16(header.fmt.BitsPerSample) / 8;
pkasting25702cb2016-01-08 13:50:27 -0800222 const size_t bytes_in_payload = ReadLE32(header.data.header.Size);
223 if (*bytes_per_sample == 0)
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000224 return false;
225 *num_samples = bytes_in_payload / *bytes_per_sample;
226
227 // Sanity check remaining fields.
228 if (ReadFourCC(header.riff.header.ID) != "RIFF")
229 return false;
230 if (ReadFourCC(header.riff.Format) != "WAVE")
231 return false;
232 if (ReadFourCC(header.fmt.header.ID) != "fmt ")
233 return false;
234 if (ReadFourCC(header.data.header.ID) != "data")
235 return false;
236
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000237 if (ReadLE32(header.riff.header.Size) < RiffChunkSize(bytes_in_payload))
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000238 return false;
239 if (ReadLE32(header.fmt.ByteRate) !=
240 ByteRate(*num_channels, *sample_rate, *bytes_per_sample))
241 return false;
242 if (ReadLE16(header.fmt.BlockAlign) !=
243 BlockAlign(*num_channels, *bytes_per_sample))
244 return false;
245
246 return CheckWavParameters(*num_channels, *sample_rate, *format,
247 *bytes_per_sample, *num_samples);
248}
249
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000250} // namespace webrtc