blob: 3182b1ffca62d5789a10ceeb97b14eda991cd889 [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
15#include "webrtc/common_audio/wav_header.h"
16
17#include <algorithm>
18#include <cstring>
19#include <limits>
20
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000021#include "webrtc/base/checks.h"
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000022#include "webrtc/common_audio/include/audio_util.h"
23
24namespace webrtc {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000025namespace {
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000026
27struct ChunkHeader {
28 uint32_t ID;
29 uint32_t Size;
30};
31COMPILE_ASSERT(sizeof(ChunkHeader) == 8, chunk_header_size);
32
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +000033// We can't nest this definition in WavHeader, because VS2013 gives an error
34// on sizeof(WavHeader::fmt): "error C2070: 'unknown': illegal sizeof operand".
35struct FmtSubchunk {
36 ChunkHeader header;
37 uint16_t AudioFormat;
38 uint16_t NumChannels;
39 uint32_t SampleRate;
40 uint32_t ByteRate;
41 uint16_t BlockAlign;
42 uint16_t BitsPerSample;
43};
44COMPILE_ASSERT(sizeof(FmtSubchunk) == 24, fmt_subchunk_size);
45const uint32_t kFmtSubchunkSize = sizeof(FmtSubchunk) - sizeof(ChunkHeader);
46
47struct WavHeader {
48 struct {
49 ChunkHeader header;
50 uint32_t Format;
51 } riff;
52 FmtSubchunk fmt;
53 struct {
54 ChunkHeader header;
55 } data;
56};
57COMPILE_ASSERT(sizeof(WavHeader) == kWavHeaderSize, no_padding_in_header);
58
59} // namespace
60
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +000061bool CheckWavParameters(int num_channels,
62 int sample_rate,
63 WavFormat format,
64 int bytes_per_sample,
65 uint32_t num_samples) {
66 // num_channels, sample_rate, and bytes_per_sample must be positive, must fit
67 // in their respective fields, and their product must fit in the 32-bit
68 // ByteRate field.
69 if (num_channels <= 0 || sample_rate <= 0 || bytes_per_sample <= 0)
70 return false;
71 if (static_cast<uint64_t>(sample_rate) > std::numeric_limits<uint32_t>::max())
72 return false;
73 if (static_cast<uint64_t>(num_channels) >
74 std::numeric_limits<uint16_t>::max())
75 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.
101 const uint32_t max_samples =
102 (std::numeric_limits<uint32_t>::max()
103 - (kWavHeaderSize - sizeof(ChunkHeader))) /
104 bytes_per_sample;
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
116static inline void WriteLE16(uint16_t* f, uint16_t x) { *f = x; }
117static inline void WriteLE32(uint32_t* f, uint32_t x) { *f = x; }
118static 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.orga3ed7132014-10-31 21:51:03 +0000124
125static inline uint16_t ReadLE16(uint16_t x) { return x; }
126static inline uint32_t ReadLE32(uint32_t x) { return x; }
127static inline std::string ReadFourCC(uint32_t x) {
128 return std::string(reinterpret_cast<char*>(&x), 4);
129}
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000130#else
131#error "Write be-to-le conversion functions"
132#endif
133
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000134static inline uint32_t RiffChunkSize(uint32_t bytes_in_payload) {
135 return bytes_in_payload + kWavHeaderSize - sizeof(ChunkHeader);
136}
137
138static inline uint32_t ByteRate(int num_channels, int sample_rate,
139 int bytes_per_sample) {
140 return static_cast<uint32_t>(num_channels) * sample_rate * bytes_per_sample;
141}
142
143static inline uint16_t BlockAlign(int num_channels, int bytes_per_sample) {
144 return num_channels * bytes_per_sample;
145}
146
147bool WriteWavHeader(uint8_t* buf,
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000148 int num_channels,
149 int sample_rate,
150 WavFormat format,
151 int bytes_per_sample,
152 uint32_t num_samples) {
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000153 if (!CheckWavParameters(num_channels, sample_rate, format,
154 bytes_per_sample, num_samples))
155 return false;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000156
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000157 WavHeader header;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000158 const uint32_t bytes_in_payload = bytes_per_sample * num_samples;
159
160 WriteFourCC(&header.riff.header.ID, 'R', 'I', 'F', 'F');
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000161 WriteLE32(&header.riff.header.Size, RiffChunkSize(bytes_in_payload));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000162 WriteFourCC(&header.riff.Format, 'W', 'A', 'V', 'E');
163
164 WriteFourCC(&header.fmt.header.ID, 'f', 'm', 't', ' ');
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000165 WriteLE32(&header.fmt.header.Size, kFmtSubchunkSize);
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000166 WriteLE16(&header.fmt.AudioFormat, format);
167 WriteLE16(&header.fmt.NumChannels, num_channels);
168 WriteLE32(&header.fmt.SampleRate, sample_rate);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000169 WriteLE32(&header.fmt.ByteRate, ByteRate(num_channels, sample_rate,
170 bytes_per_sample));
171 WriteLE16(&header.fmt.BlockAlign, BlockAlign(num_channels, bytes_per_sample));
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000172 WriteLE16(&header.fmt.BitsPerSample, 8 * bytes_per_sample);
173
174 WriteFourCC(&header.data.header.ID, 'd', 'a', 't', 'a');
175 WriteLE32(&header.data.header.Size, bytes_in_payload);
176
177 // Do an extra copy rather than writing everything to buf directly, since buf
178 // might not be correctly aligned.
179 memcpy(buf, &header, kWavHeaderSize);
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000180 return true;
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000181}
182
andrew@webrtc.orga3ed7132014-10-31 21:51:03 +0000183bool ReadWavHeader(const uint8_t* buf,
184 int* num_channels,
185 int* sample_rate,
186 WavFormat* format,
187 int* bytes_per_sample,
188 uint32_t* num_samples) {
189 WavHeader header;
190 memcpy(&header, buf, kWavHeaderSize);
191
192 // Parse needed fields.
193 *format = static_cast<WavFormat>(ReadLE16(header.fmt.AudioFormat));
194 *num_channels = ReadLE16(header.fmt.NumChannels);
195 *sample_rate = ReadLE32(header.fmt.SampleRate);
196 *bytes_per_sample = ReadLE16(header.fmt.BitsPerSample) / 8;
197 const uint32_t bytes_in_payload = ReadLE32(header.data.header.Size);
198 if (*bytes_per_sample <= 0)
199 return false;
200 *num_samples = bytes_in_payload / *bytes_per_sample;
201
202 // Sanity check remaining fields.
203 if (ReadFourCC(header.riff.header.ID) != "RIFF")
204 return false;
205 if (ReadFourCC(header.riff.Format) != "WAVE")
206 return false;
207 if (ReadFourCC(header.fmt.header.ID) != "fmt ")
208 return false;
209 if (ReadFourCC(header.data.header.ID) != "data")
210 return false;
211
212 if (ReadLE32(header.riff.header.Size) != RiffChunkSize(bytes_in_payload))
213 return false;
214 if (ReadLE32(header.fmt.header.Size) != kFmtSubchunkSize)
215 return false;
216 if (ReadLE32(header.fmt.ByteRate) !=
217 ByteRate(*num_channels, *sample_rate, *bytes_per_sample))
218 return false;
219 if (ReadLE16(header.fmt.BlockAlign) !=
220 BlockAlign(*num_channels, *bytes_per_sample))
221 return false;
222
223 return CheckWavParameters(*num_channels, *sample_rate, *format,
224 *bytes_per_sample, *num_samples);
225}
226
227
kwiberg@webrtc.org877083c2014-08-20 07:42:46 +0000228} // namespace webrtc