2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
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.
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
15 #include "webrtc/common_audio/wav_header.h"
21 #include "webrtc/base/checks.h"
22 #include "webrtc/common_audio/include/audio_util.h"
31 COMPILE_ASSERT(sizeof(ChunkHeader) == 8, chunk_header_size);
33 // We can't nest this definition in WavHeader, because VS2013 gives an error
34 // on sizeof(WavHeader::fmt): "error C2070: 'unknown': illegal sizeof operand".
42 uint16_t BitsPerSample;
44 COMPILE_ASSERT(sizeof(FmtSubchunk) == 24, fmt_subchunk_size);
45 const uint32_t kFmtSubchunkSize = sizeof(FmtSubchunk) - sizeof(ChunkHeader);
57 COMPILE_ASSERT(sizeof(WavHeader) == kWavHeaderSize, no_padding_in_header);
61 bool CheckWavParameters(int num_channels,
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
69 if (num_channels <= 0 || sample_rate <= 0 || bytes_per_sample <= 0)
71 if (static_cast<uint64_t>(sample_rate) > std::numeric_limits<uint32_t>::max())
73 if (static_cast<uint64_t>(num_channels) >
74 std::numeric_limits<uint16_t>::max())
76 if (static_cast<uint64_t>(bytes_per_sample) * 8 >
77 std::numeric_limits<uint16_t>::max())
79 if (static_cast<uint64_t>(sample_rate) * num_channels * bytes_per_sample >
80 std::numeric_limits<uint32_t>::max())
83 // format and bytes_per_sample must agree.
86 // Other values may be OK, but for now we're conservative:
87 if (bytes_per_sample != 1 && bytes_per_sample != 2)
92 if (bytes_per_sample != 1)
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))) /
105 if (num_samples > max_samples)
108 // Each channel must have the same number of samples.
109 if (num_samples % num_channels != 0)
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;
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);
131 #error "Write be-to-le conversion functions"
134 static inline uint32_t RiffChunkSize(uint32_t bytes_in_payload) {
135 return bytes_in_payload + kWavHeaderSize - sizeof(ChunkHeader);
138 static 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;
143 static inline uint16_t BlockAlign(int num_channels, int bytes_per_sample) {
144 return num_channels * bytes_per_sample;
147 void WriteWavHeader(uint8_t* buf,
151 int bytes_per_sample,
152 uint32_t num_samples) {
153 CHECK(CheckWavParameters(num_channels, sample_rate, format,
154 bytes_per_sample, num_samples));
157 const uint32_t bytes_in_payload = bytes_per_sample * num_samples;
159 WriteFourCC(&header.riff.header.ID, 'R', 'I', 'F', 'F');
160 WriteLE32(&header.riff.header.Size, RiffChunkSize(bytes_in_payload));
161 WriteFourCC(&header.riff.Format, 'W', 'A', 'V', 'E');
163 WriteFourCC(&header.fmt.header.ID, 'f', 'm', 't', ' ');
164 WriteLE32(&header.fmt.header.Size, kFmtSubchunkSize);
165 WriteLE16(&header.fmt.AudioFormat, format);
166 WriteLE16(&header.fmt.NumChannels, num_channels);
167 WriteLE32(&header.fmt.SampleRate, sample_rate);
168 WriteLE32(&header.fmt.ByteRate, ByteRate(num_channels, sample_rate,
170 WriteLE16(&header.fmt.BlockAlign, BlockAlign(num_channels, bytes_per_sample));
171 WriteLE16(&header.fmt.BitsPerSample, 8 * bytes_per_sample);
173 WriteFourCC(&header.data.header.ID, 'd', 'a', 't', 'a');
174 WriteLE32(&header.data.header.Size, bytes_in_payload);
176 // Do an extra copy rather than writing everything to buf directly, since buf
177 // might not be correctly aligned.
178 memcpy(buf, &header, kWavHeaderSize);
181 bool ReadWavHeader(const uint8_t* buf,
185 int* bytes_per_sample,
186 uint32_t* num_samples) {
188 memcpy(&header, buf, kWavHeaderSize);
190 // Parse needed fields.
191 *format = static_cast<WavFormat>(ReadLE16(header.fmt.AudioFormat));
192 *num_channels = ReadLE16(header.fmt.NumChannels);
193 *sample_rate = ReadLE32(header.fmt.SampleRate);
194 *bytes_per_sample = ReadLE16(header.fmt.BitsPerSample) / 8;
195 const uint32_t bytes_in_payload = ReadLE32(header.data.header.Size);
196 if (*bytes_per_sample <= 0)
198 *num_samples = bytes_in_payload / *bytes_per_sample;
200 // Sanity check remaining fields.
201 if (ReadFourCC(header.riff.header.ID) != "RIFF")
203 if (ReadFourCC(header.riff.Format) != "WAVE")
205 if (ReadFourCC(header.fmt.header.ID) != "fmt ")
207 if (ReadFourCC(header.data.header.ID) != "data")
210 if (ReadLE32(header.riff.header.Size) != RiffChunkSize(bytes_in_payload))
212 if (ReadLE32(header.fmt.header.Size) != kFmtSubchunkSize)
214 if (ReadLE32(header.fmt.ByteRate) !=
215 ByteRate(*num_channels, *sample_rate, *bytes_per_sample))
217 if (ReadLE16(header.fmt.BlockAlign) !=
218 BlockAlign(*num_channels, *bytes_per_sample))
221 return CheckWavParameters(*num_channels, *sample_rate, *format,
222 *bytes_per_sample, *num_samples);
226 } // namespace webrtc