blob: d093fa011c3c0dfe1deeb8b35f03b508175b7f24 [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"
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000024
25namespace webrtc {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000026namespace {
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000027
28struct ChunkHeader {
29 uint32_t ID;
30 uint32_t Size;
31};
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +000032static_assert(sizeof(ChunkHeader) == 8, "ChunkHeader size");
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000033
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000034// We can't nest this definition in WavHeader, because VS2013 gives an error
35// on sizeof(WavHeader::fmt): "error C2070: 'unknown': illegal sizeof operand".
36struct 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.org2ebfac52015-01-14 10:51:54 +000045static_assert(sizeof(FmtSubchunk) == 24, "FmtSubchunk size");
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000046const uint32_t kFmtSubchunkSize = sizeof(FmtSubchunk) - sizeof(ChunkHeader);
47
48struct 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.org2ebfac52015-01-14 10:51:54 +000058static_assert(sizeof(WavHeader) == kWavHeaderSize, "no padding in header");
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000059
60} // namespace
61
Peter Kasting69558702016-01-12 16:26:35 -080062bool CheckWavParameters(size_t num_channels,
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000063 int sample_rate,
64 WavFormat format,
pkasting25702cb2016-01-08 13:50:27 -080065 size_t bytes_per_sample,
66 size_t num_samples) {
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000067 // 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.
Peter Kasting69558702016-01-12 16:26:35 -080070 if (num_channels == 0 || sample_rate <= 0 || bytes_per_sample == 0)
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000071 return false;
72 if (static_cast<uint64_t>(sample_rate) > std::numeric_limits<uint32_t>::max())
73 return false;
Peter Kasting69558702016-01-12 16:26:35 -080074 if (num_channels > std::numeric_limits<uint16_t>::max())
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000075 return false;
76 if (static_cast<uint64_t>(bytes_per_sample) * 8 >
77 std::numeric_limits<uint16_t>::max())
78 return false;
79 if (static_cast<uint64_t>(sample_rate) * num_channels * bytes_per_sample >
80 std::numeric_limits<uint32_t>::max())
81 return false;
82
83 // format and bytes_per_sample must agree.
84 switch (format) {
85 case kWavFormatPcm:
86 // Other values may be OK, but for now we're conservative:
87 if (bytes_per_sample != 1 && bytes_per_sample != 2)
88 return false;
89 break;
90 case kWavFormatALaw:
91 case kWavFormatMuLaw:
92 if (bytes_per_sample != 1)
93 return false;
94 break;
95 default:
96 return false;
97 }
98
99 // The number of bytes in the file, not counting the first ChunkHeader, must
100 // be less than 2^32; otherwise, the ChunkSize field overflows.
pkasting25702cb2016-01-08 13:50:27 -0800101 const size_t header_size = kWavHeaderSize - sizeof(ChunkHeader);
102 const size_t max_samples =
103 (std::numeric_limits<uint32_t>::max() - header_size) / bytes_per_sample;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000104 if (num_samples > max_samples)
105 return false;
106
107 // Each channel must have the same number of samples.
108 if (num_samples % num_channels != 0)
109 return false;
110
111 return true;
112}
113
114#ifdef WEBRTC_ARCH_LITTLE_ENDIAN
Yves Gerey665174f2018-06-19 15:03:05 +0200115static inline void WriteLE16(uint16_t* f, uint16_t x) {
116 *f = x;
117}
118static inline void WriteLE32(uint32_t* f, uint32_t x) {
119 *f = x;
120}
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000121static inline void WriteFourCC(uint32_t* f, char a, char b, char c, char d) {
Yves Gerey665174f2018-06-19 15:03:05 +0200122 *f = static_cast<uint32_t>(a) | static_cast<uint32_t>(b) << 8 |
123 static_cast<uint32_t>(c) << 16 | static_cast<uint32_t>(d) << 24;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000124}
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000125
Yves Gerey665174f2018-06-19 15:03:05 +0200126static inline uint16_t ReadLE16(uint16_t x) {
127 return x;
128}
129static inline uint32_t ReadLE32(uint32_t x) {
130 return x;
131}
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000132static inline std::string ReadFourCC(uint32_t x) {
133 return std::string(reinterpret_cast<char*>(&x), 4);
134}
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000135#else
136#error "Write be-to-le conversion functions"
137#endif
138
pkasting25702cb2016-01-08 13:50:27 -0800139static inline uint32_t RiffChunkSize(size_t bytes_in_payload) {
Yves Gerey665174f2018-06-19 15:03:05 +0200140 return static_cast<uint32_t>(bytes_in_payload + kWavHeaderSize -
141 sizeof(ChunkHeader));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000142}
143
Yves Gerey665174f2018-06-19 15:03:05 +0200144static inline uint32_t ByteRate(size_t num_channels,
145 int sample_rate,
pkasting25702cb2016-01-08 13:50:27 -0800146 size_t bytes_per_sample) {
147 return static_cast<uint32_t>(num_channels * sample_rate * bytes_per_sample);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000148}
149
Peter Kasting69558702016-01-12 16:26:35 -0800150static inline uint16_t BlockAlign(size_t num_channels,
151 size_t bytes_per_sample) {
pkasting25702cb2016-01-08 13:50:27 -0800152 return static_cast<uint16_t>(num_channels * bytes_per_sample);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000153}
154
andrew@webrtc.orgf866b2d2014-11-03 18:20:06 +0000155void WriteWavHeader(uint8_t* buf,
Peter Kasting69558702016-01-12 16:26:35 -0800156 size_t num_channels,
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000157 int sample_rate,
158 WavFormat format,
pkasting25702cb2016-01-08 13:50:27 -0800159 size_t bytes_per_sample,
160 size_t num_samples) {
henrikg91d6ede2015-09-17 00:24:34 -0700161 RTC_CHECK(CheckWavParameters(num_channels, sample_rate, format,
162 bytes_per_sample, num_samples));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000163
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000164 WavHeader header;
pkasting25702cb2016-01-08 13:50:27 -0800165 const size_t bytes_in_payload = bytes_per_sample * num_samples;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000166
167 WriteFourCC(&header.riff.header.ID, 'R', 'I', 'F', 'F');
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000168 WriteLE32(&header.riff.header.Size, RiffChunkSize(bytes_in_payload));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000169 WriteFourCC(&header.riff.Format, 'W', 'A', 'V', 'E');
170
171 WriteFourCC(&header.fmt.header.ID, 'f', 'm', 't', ' ');
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000172 WriteLE32(&header.fmt.header.Size, kFmtSubchunkSize);
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000173 WriteLE16(&header.fmt.AudioFormat, format);
pkasting25702cb2016-01-08 13:50:27 -0800174 WriteLE16(&header.fmt.NumChannels, static_cast<uint16_t>(num_channels));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000175 WriteLE32(&header.fmt.SampleRate, sample_rate);
Yves Gerey665174f2018-06-19 15:03:05 +0200176 WriteLE32(&header.fmt.ByteRate,
177 ByteRate(num_channels, sample_rate, bytes_per_sample));
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000178 WriteLE16(&header.fmt.BlockAlign, BlockAlign(num_channels, bytes_per_sample));
pkasting25702cb2016-01-08 13:50:27 -0800179 WriteLE16(&header.fmt.BitsPerSample,
180 static_cast<uint16_t>(8 * bytes_per_sample));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000181
182 WriteFourCC(&header.data.header.ID, 'd', 'a', 't', 'a');
pkasting25702cb2016-01-08 13:50:27 -0800183 WriteLE32(&header.data.header.Size, static_cast<uint32_t>(bytes_in_payload));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000184
185 // Do an extra copy rather than writing everything to buf directly, since buf
186 // might not be correctly aligned.
187 memcpy(buf, &header, kWavHeaderSize);
188}
189
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000190bool ReadWavHeader(ReadableWav* readable,
Peter Kasting69558702016-01-12 16:26:35 -0800191 size_t* num_channels,
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000192 int* sample_rate,
193 WavFormat* format,
pkasting25702cb2016-01-08 13:50:27 -0800194 size_t* bytes_per_sample,
195 size_t* num_samples) {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000196 WavHeader header;
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000197 if (readable->Read(&header, kWavHeaderSize - sizeof(header.data)) !=
198 kWavHeaderSize - sizeof(header.data))
199 return false;
200
201 const uint32_t fmt_size = ReadLE32(header.fmt.header.Size);
202 if (fmt_size != kFmtSubchunkSize) {
203 // There is an optional two-byte extension field permitted to be present
204 // with PCM, but which must be zero.
205 int16_t ext_size;
206 if (kFmtSubchunkSize + sizeof(ext_size) != fmt_size)
207 return false;
208 if (readable->Read(&ext_size, sizeof(ext_size)) != sizeof(ext_size))
209 return false;
210 if (ext_size != 0)
211 return false;
212 }
213 if (readable->Read(&header.data, sizeof(header.data)) != sizeof(header.data))
214 return false;
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000215
216 // Parse needed fields.
217 *format = static_cast<WavFormat>(ReadLE16(header.fmt.AudioFormat));
218 *num_channels = ReadLE16(header.fmt.NumChannels);
219 *sample_rate = ReadLE32(header.fmt.SampleRate);
220 *bytes_per_sample = ReadLE16(header.fmt.BitsPerSample) / 8;
pkasting25702cb2016-01-08 13:50:27 -0800221 const size_t bytes_in_payload = ReadLE32(header.data.header.Size);
222 if (*bytes_per_sample == 0)
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000223 return false;
224 *num_samples = bytes_in_payload / *bytes_per_sample;
225
226 // Sanity check remaining fields.
227 if (ReadFourCC(header.riff.header.ID) != "RIFF")
228 return false;
229 if (ReadFourCC(header.riff.Format) != "WAVE")
230 return false;
231 if (ReadFourCC(header.fmt.header.ID) != "fmt ")
232 return false;
233 if (ReadFourCC(header.data.header.ID) != "data")
234 return false;
235
andrew@webrtc.org048c5022014-12-16 20:17:21 +0000236 if (ReadLE32(header.riff.header.Size) < RiffChunkSize(bytes_in_payload))
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000237 return false;
238 if (ReadLE32(header.fmt.ByteRate) !=
239 ByteRate(*num_channels, *sample_rate, *bytes_per_sample))
240 return false;
241 if (ReadLE16(header.fmt.BlockAlign) !=
242 BlockAlign(*num_channels, *bytes_per_sample))
243 return false;
244
245 return CheckWavParameters(*num_channels, *sample_rate, *format,
246 *bytes_per_sample, *num_samples);
247}
248
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000249} // namespace webrtc