Update to current webrtc library
This is from the upstream library commit id 3326535126e435f1ba647885ce43a8f0f3d317eb, corresponding to Chromium 88.0.4290.1.
This commit is contained in:
@ -12,28 +12,42 @@
|
||||
// https://ccrma.stanford.edu/courses/422/projects/WaveFormat/ and
|
||||
// http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
|
||||
|
||||
#include "webrtc/common_audio/wav_header.h"
|
||||
#include "common_audio/wav_header.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
|
||||
#include "webrtc/base/checks.h"
|
||||
#include "webrtc/common_audio/include/audio_util.h"
|
||||
#include "rtc_base/checks.h"
|
||||
#include "rtc_base/logging.h"
|
||||
#include "rtc_base/sanitizer.h"
|
||||
#include "rtc_base/system/arch.h"
|
||||
|
||||
namespace webrtc {
|
||||
namespace {
|
||||
|
||||
#ifndef WEBRTC_ARCH_LITTLE_ENDIAN
|
||||
#error "Code not working properly for big endian platforms."
|
||||
#endif
|
||||
|
||||
#pragma pack(2)
|
||||
struct ChunkHeader {
|
||||
uint32_t ID;
|
||||
uint32_t Size;
|
||||
};
|
||||
static_assert(sizeof(ChunkHeader) == 8, "ChunkHeader size");
|
||||
|
||||
#pragma pack(2)
|
||||
struct RiffHeader {
|
||||
ChunkHeader header;
|
||||
uint32_t Format;
|
||||
};
|
||||
static_assert(sizeof(RiffHeader) == sizeof(ChunkHeader) + 4, "RiffHeader size");
|
||||
|
||||
// We can't nest this definition in WavHeader, because VS2013 gives an error
|
||||
// on sizeof(WavHeader::fmt): "error C2070: 'unknown': illegal sizeof operand".
|
||||
struct FmtSubchunk {
|
||||
#pragma pack(2)
|
||||
struct FmtPcmSubchunk {
|
||||
ChunkHeader header;
|
||||
uint16_t AudioFormat;
|
||||
uint16_t NumChannels;
|
||||
@ -42,37 +56,247 @@ struct FmtSubchunk {
|
||||
uint16_t BlockAlign;
|
||||
uint16_t BitsPerSample;
|
||||
};
|
||||
static_assert(sizeof(FmtSubchunk) == 24, "FmtSubchunk size");
|
||||
const uint32_t kFmtSubchunkSize = sizeof(FmtSubchunk) - sizeof(ChunkHeader);
|
||||
static_assert(sizeof(FmtPcmSubchunk) == 24, "FmtPcmSubchunk size");
|
||||
const uint32_t kFmtPcmSubchunkSize =
|
||||
sizeof(FmtPcmSubchunk) - sizeof(ChunkHeader);
|
||||
|
||||
struct WavHeader {
|
||||
struct {
|
||||
ChunkHeader header;
|
||||
uint32_t Format;
|
||||
} riff;
|
||||
FmtSubchunk fmt;
|
||||
// Pack struct to avoid additional padding bytes.
|
||||
#pragma pack(2)
|
||||
struct FmtIeeeFloatSubchunk {
|
||||
ChunkHeader header;
|
||||
uint16_t AudioFormat;
|
||||
uint16_t NumChannels;
|
||||
uint32_t SampleRate;
|
||||
uint32_t ByteRate;
|
||||
uint16_t BlockAlign;
|
||||
uint16_t BitsPerSample;
|
||||
uint16_t ExtensionSize;
|
||||
};
|
||||
static_assert(sizeof(FmtIeeeFloatSubchunk) == 26, "FmtIeeeFloatSubchunk size");
|
||||
const uint32_t kFmtIeeeFloatSubchunkSize =
|
||||
sizeof(FmtIeeeFloatSubchunk) - sizeof(ChunkHeader);
|
||||
|
||||
// Simple PCM wav header. It does not include chunks that are not essential to
|
||||
// read audio samples.
|
||||
#pragma pack(2)
|
||||
struct WavHeaderPcm {
|
||||
WavHeaderPcm(const WavHeaderPcm&) = default;
|
||||
WavHeaderPcm& operator=(const WavHeaderPcm&) = default;
|
||||
RiffHeader riff;
|
||||
FmtPcmSubchunk fmt;
|
||||
struct {
|
||||
ChunkHeader header;
|
||||
} data;
|
||||
};
|
||||
static_assert(sizeof(WavHeader) == kWavHeaderSize, "no padding in header");
|
||||
static_assert(sizeof(WavHeaderPcm) == kPcmWavHeaderSize,
|
||||
"no padding in header");
|
||||
|
||||
} // namespace
|
||||
// IEEE Float Wav header, includes extra chunks necessary for proper non-PCM
|
||||
// WAV implementation.
|
||||
#pragma pack(2)
|
||||
struct WavHeaderIeeeFloat {
|
||||
WavHeaderIeeeFloat(const WavHeaderIeeeFloat&) = default;
|
||||
WavHeaderIeeeFloat& operator=(const WavHeaderIeeeFloat&) = default;
|
||||
RiffHeader riff;
|
||||
FmtIeeeFloatSubchunk fmt;
|
||||
struct {
|
||||
ChunkHeader header;
|
||||
uint32_t SampleLength;
|
||||
} fact;
|
||||
struct {
|
||||
ChunkHeader header;
|
||||
} data;
|
||||
};
|
||||
static_assert(sizeof(WavHeaderIeeeFloat) == kIeeeFloatWavHeaderSize,
|
||||
"no padding in header");
|
||||
|
||||
bool CheckWavParameters(int num_channels,
|
||||
uint32_t PackFourCC(char a, char b, char c, char d) {
|
||||
uint32_t packed_value =
|
||||
static_cast<uint32_t>(a) | static_cast<uint32_t>(b) << 8 |
|
||||
static_cast<uint32_t>(c) << 16 | static_cast<uint32_t>(d) << 24;
|
||||
return packed_value;
|
||||
}
|
||||
|
||||
std::string ReadFourCC(uint32_t x) {
|
||||
return std::string(reinterpret_cast<char*>(&x), 4);
|
||||
}
|
||||
|
||||
uint16_t MapWavFormatToHeaderField(WavFormat format) {
|
||||
switch (format) {
|
||||
case WavFormat::kWavFormatPcm:
|
||||
return 1;
|
||||
case WavFormat::kWavFormatIeeeFloat:
|
||||
return 3;
|
||||
case WavFormat::kWavFormatALaw:
|
||||
return 6;
|
||||
case WavFormat::kWavFormatMuLaw:
|
||||
return 7;
|
||||
}
|
||||
RTC_CHECK(false);
|
||||
}
|
||||
|
||||
WavFormat MapHeaderFieldToWavFormat(uint16_t format_header_value) {
|
||||
if (format_header_value == 1) {
|
||||
return WavFormat::kWavFormatPcm;
|
||||
}
|
||||
if (format_header_value == 3) {
|
||||
return WavFormat::kWavFormatIeeeFloat;
|
||||
}
|
||||
|
||||
RTC_CHECK(false) << "Unsupported WAV format";
|
||||
}
|
||||
|
||||
uint32_t RiffChunkSize(size_t bytes_in_payload, size_t header_size) {
|
||||
return static_cast<uint32_t>(bytes_in_payload + header_size -
|
||||
sizeof(ChunkHeader));
|
||||
}
|
||||
|
||||
uint32_t ByteRate(size_t num_channels,
|
||||
int sample_rate,
|
||||
size_t bytes_per_sample) {
|
||||
return static_cast<uint32_t>(num_channels * sample_rate * bytes_per_sample);
|
||||
}
|
||||
|
||||
uint16_t BlockAlign(size_t num_channels, size_t bytes_per_sample) {
|
||||
return static_cast<uint16_t>(num_channels * bytes_per_sample);
|
||||
}
|
||||
|
||||
// Finds a chunk having the sought ID. If found, then |readable| points to the
|
||||
// first byte of the sought chunk data. If not found, the end of the file is
|
||||
// reached.
|
||||
bool FindWaveChunk(ChunkHeader* chunk_header,
|
||||
WavHeaderReader* readable,
|
||||
const std::string sought_chunk_id) {
|
||||
RTC_DCHECK_EQ(sought_chunk_id.size(), 4);
|
||||
while (true) {
|
||||
if (readable->Read(chunk_header, sizeof(*chunk_header)) !=
|
||||
sizeof(*chunk_header))
|
||||
return false; // EOF.
|
||||
if (ReadFourCC(chunk_header->ID) == sought_chunk_id)
|
||||
return true; // Sought chunk found.
|
||||
// Ignore current chunk by skipping its payload.
|
||||
if (!readable->SeekForward(chunk_header->Size))
|
||||
return false; // EOF or error.
|
||||
}
|
||||
}
|
||||
|
||||
bool ReadFmtChunkData(FmtPcmSubchunk* fmt_subchunk, WavHeaderReader* readable) {
|
||||
// Reads "fmt " chunk payload.
|
||||
if (readable->Read(&(fmt_subchunk->AudioFormat), kFmtPcmSubchunkSize) !=
|
||||
kFmtPcmSubchunkSize)
|
||||
return false;
|
||||
const uint32_t fmt_size = fmt_subchunk->header.Size;
|
||||
if (fmt_size != kFmtPcmSubchunkSize) {
|
||||
// There is an optional two-byte extension field permitted to be present
|
||||
// with PCM, but which must be zero.
|
||||
int16_t ext_size;
|
||||
if (kFmtPcmSubchunkSize + sizeof(ext_size) != fmt_size)
|
||||
return false;
|
||||
if (readable->Read(&ext_size, sizeof(ext_size)) != sizeof(ext_size))
|
||||
return false;
|
||||
if (ext_size != 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void WritePcmWavHeader(size_t num_channels,
|
||||
int sample_rate,
|
||||
size_t bytes_per_sample,
|
||||
size_t num_samples,
|
||||
uint8_t* buf,
|
||||
size_t* header_size) {
|
||||
RTC_CHECK(buf);
|
||||
RTC_CHECK(header_size);
|
||||
*header_size = kPcmWavHeaderSize;
|
||||
auto header = rtc::MsanUninitialized<WavHeaderPcm>({});
|
||||
const size_t bytes_in_payload = bytes_per_sample * num_samples;
|
||||
|
||||
header.riff.header.ID = PackFourCC('R', 'I', 'F', 'F');
|
||||
header.riff.header.Size = RiffChunkSize(bytes_in_payload, *header_size);
|
||||
header.riff.Format = PackFourCC('W', 'A', 'V', 'E');
|
||||
header.fmt.header.ID = PackFourCC('f', 'm', 't', ' ');
|
||||
header.fmt.header.Size = kFmtPcmSubchunkSize;
|
||||
header.fmt.AudioFormat = MapWavFormatToHeaderField(WavFormat::kWavFormatPcm);
|
||||
header.fmt.NumChannels = static_cast<uint16_t>(num_channels);
|
||||
header.fmt.SampleRate = sample_rate;
|
||||
header.fmt.ByteRate = ByteRate(num_channels, sample_rate, bytes_per_sample);
|
||||
header.fmt.BlockAlign = BlockAlign(num_channels, bytes_per_sample);
|
||||
header.fmt.BitsPerSample = static_cast<uint16_t>(8 * bytes_per_sample);
|
||||
header.data.header.ID = PackFourCC('d', 'a', 't', 'a');
|
||||
header.data.header.Size = static_cast<uint32_t>(bytes_in_payload);
|
||||
|
||||
// Do an extra copy rather than writing everything to buf directly, since buf
|
||||
// might not be correctly aligned.
|
||||
memcpy(buf, &header, *header_size);
|
||||
}
|
||||
|
||||
void WriteIeeeFloatWavHeader(size_t num_channels,
|
||||
int sample_rate,
|
||||
size_t bytes_per_sample,
|
||||
size_t num_samples,
|
||||
uint8_t* buf,
|
||||
size_t* header_size) {
|
||||
RTC_CHECK(buf);
|
||||
RTC_CHECK(header_size);
|
||||
*header_size = kIeeeFloatWavHeaderSize;
|
||||
auto header = rtc::MsanUninitialized<WavHeaderIeeeFloat>({});
|
||||
const size_t bytes_in_payload = bytes_per_sample * num_samples;
|
||||
|
||||
header.riff.header.ID = PackFourCC('R', 'I', 'F', 'F');
|
||||
header.riff.header.Size = RiffChunkSize(bytes_in_payload, *header_size);
|
||||
header.riff.Format = PackFourCC('W', 'A', 'V', 'E');
|
||||
header.fmt.header.ID = PackFourCC('f', 'm', 't', ' ');
|
||||
header.fmt.header.Size = kFmtIeeeFloatSubchunkSize;
|
||||
header.fmt.AudioFormat =
|
||||
MapWavFormatToHeaderField(WavFormat::kWavFormatIeeeFloat);
|
||||
header.fmt.NumChannels = static_cast<uint16_t>(num_channels);
|
||||
header.fmt.SampleRate = sample_rate;
|
||||
header.fmt.ByteRate = ByteRate(num_channels, sample_rate, bytes_per_sample);
|
||||
header.fmt.BlockAlign = BlockAlign(num_channels, bytes_per_sample);
|
||||
header.fmt.BitsPerSample = static_cast<uint16_t>(8 * bytes_per_sample);
|
||||
header.fmt.ExtensionSize = 0;
|
||||
header.fact.header.ID = PackFourCC('f', 'a', 'c', 't');
|
||||
header.fact.header.Size = 4;
|
||||
header.fact.SampleLength = static_cast<uint32_t>(num_channels * num_samples);
|
||||
header.data.header.ID = PackFourCC('d', 'a', 't', 'a');
|
||||
header.data.header.Size = static_cast<uint32_t>(bytes_in_payload);
|
||||
|
||||
// Do an extra copy rather than writing everything to buf directly, since buf
|
||||
// might not be correctly aligned.
|
||||
memcpy(buf, &header, *header_size);
|
||||
}
|
||||
|
||||
// Returns the number of bytes per sample for the format.
|
||||
size_t GetFormatBytesPerSample(WavFormat format) {
|
||||
switch (format) {
|
||||
case WavFormat::kWavFormatPcm:
|
||||
// Other values may be OK, but for now we're conservative.
|
||||
return 2;
|
||||
case WavFormat::kWavFormatALaw:
|
||||
case WavFormat::kWavFormatMuLaw:
|
||||
return 1;
|
||||
case WavFormat::kWavFormatIeeeFloat:
|
||||
return 4;
|
||||
default:
|
||||
RTC_CHECK(false);
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
bool CheckWavParameters(size_t num_channels,
|
||||
int sample_rate,
|
||||
WavFormat format,
|
||||
int bytes_per_sample,
|
||||
uint32_t num_samples) {
|
||||
size_t bytes_per_sample,
|
||||
size_t num_samples) {
|
||||
// num_channels, sample_rate, and bytes_per_sample must be positive, must fit
|
||||
// in their respective fields, and their product must fit in the 32-bit
|
||||
// ByteRate field.
|
||||
if (num_channels <= 0 || sample_rate <= 0 || bytes_per_sample <= 0)
|
||||
if (num_channels == 0 || sample_rate <= 0 || bytes_per_sample == 0)
|
||||
return false;
|
||||
if (static_cast<uint64_t>(sample_rate) > std::numeric_limits<uint32_t>::max())
|
||||
return false;
|
||||
if (static_cast<uint64_t>(num_channels) >
|
||||
std::numeric_limits<uint16_t>::max())
|
||||
if (num_channels > std::numeric_limits<uint16_t>::max())
|
||||
return false;
|
||||
if (static_cast<uint64_t>(bytes_per_sample) * 8 >
|
||||
std::numeric_limits<uint16_t>::max())
|
||||
@ -83,26 +307,29 @@ bool CheckWavParameters(int num_channels,
|
||||
|
||||
// format and bytes_per_sample must agree.
|
||||
switch (format) {
|
||||
case kWavFormatPcm:
|
||||
case WavFormat::kWavFormatPcm:
|
||||
// Other values may be OK, but for now we're conservative:
|
||||
if (bytes_per_sample != 1 && bytes_per_sample != 2)
|
||||
return false;
|
||||
break;
|
||||
case kWavFormatALaw:
|
||||
case kWavFormatMuLaw:
|
||||
case WavFormat::kWavFormatALaw:
|
||||
case WavFormat::kWavFormatMuLaw:
|
||||
if (bytes_per_sample != 1)
|
||||
return false;
|
||||
break;
|
||||
case WavFormat::kWavFormatIeeeFloat:
|
||||
if (bytes_per_sample != 4)
|
||||
return false;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
// The number of bytes in the file, not counting the first ChunkHeader, must
|
||||
// be less than 2^32; otherwise, the ChunkSize field overflows.
|
||||
const uint32_t max_samples =
|
||||
(std::numeric_limits<uint32_t>::max()
|
||||
- (kWavHeaderSize - sizeof(ChunkHeader))) /
|
||||
bytes_per_sample;
|
||||
const size_t header_size = kPcmWavHeaderSize - sizeof(ChunkHeader);
|
||||
const size_t max_samples =
|
||||
(std::numeric_limits<uint32_t>::max() - header_size) / bytes_per_sample;
|
||||
if (num_samples > max_samples)
|
||||
return false;
|
||||
|
||||
@ -113,130 +340,102 @@ bool CheckWavParameters(int num_channels,
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef WEBRTC_ARCH_LITTLE_ENDIAN
|
||||
static inline void WriteLE16(uint16_t* f, uint16_t x) { *f = x; }
|
||||
static inline void WriteLE32(uint32_t* f, uint32_t x) { *f = x; }
|
||||
static inline void WriteFourCC(uint32_t* f, char a, char b, char c, char d) {
|
||||
*f = static_cast<uint32_t>(a)
|
||||
| static_cast<uint32_t>(b) << 8
|
||||
| static_cast<uint32_t>(c) << 16
|
||||
| static_cast<uint32_t>(d) << 24;
|
||||
} // namespace
|
||||
|
||||
bool CheckWavParameters(size_t num_channels,
|
||||
int sample_rate,
|
||||
WavFormat format,
|
||||
size_t num_samples) {
|
||||
return CheckWavParameters(num_channels, sample_rate, format,
|
||||
GetFormatBytesPerSample(format), num_samples);
|
||||
}
|
||||
|
||||
static inline uint16_t ReadLE16(uint16_t x) { return x; }
|
||||
static inline uint32_t ReadLE32(uint32_t x) { return x; }
|
||||
static inline std::string ReadFourCC(uint32_t x) {
|
||||
return std::string(reinterpret_cast<char*>(&x), 4);
|
||||
}
|
||||
#else
|
||||
#error "Write be-to-le conversion functions"
|
||||
#endif
|
||||
|
||||
static inline uint32_t RiffChunkSize(uint32_t bytes_in_payload) {
|
||||
return bytes_in_payload + kWavHeaderSize - sizeof(ChunkHeader);
|
||||
}
|
||||
|
||||
static inline uint32_t ByteRate(int num_channels, int sample_rate,
|
||||
int bytes_per_sample) {
|
||||
return static_cast<uint32_t>(num_channels) * sample_rate * bytes_per_sample;
|
||||
}
|
||||
|
||||
static inline uint16_t BlockAlign(int num_channels, int bytes_per_sample) {
|
||||
return num_channels * bytes_per_sample;
|
||||
}
|
||||
|
||||
void WriteWavHeader(uint8_t* buf,
|
||||
int num_channels,
|
||||
void WriteWavHeader(size_t num_channels,
|
||||
int sample_rate,
|
||||
WavFormat format,
|
||||
int bytes_per_sample,
|
||||
uint32_t num_samples) {
|
||||
size_t num_samples,
|
||||
uint8_t* buf,
|
||||
size_t* header_size) {
|
||||
RTC_CHECK(buf);
|
||||
RTC_CHECK(header_size);
|
||||
|
||||
const size_t bytes_per_sample = GetFormatBytesPerSample(format);
|
||||
RTC_CHECK(CheckWavParameters(num_channels, sample_rate, format,
|
||||
bytes_per_sample, num_samples));
|
||||
|
||||
WavHeader header;
|
||||
const uint32_t bytes_in_payload = bytes_per_sample * num_samples;
|
||||
|
||||
WriteFourCC(&header.riff.header.ID, 'R', 'I', 'F', 'F');
|
||||
WriteLE32(&header.riff.header.Size, RiffChunkSize(bytes_in_payload));
|
||||
WriteFourCC(&header.riff.Format, 'W', 'A', 'V', 'E');
|
||||
|
||||
WriteFourCC(&header.fmt.header.ID, 'f', 'm', 't', ' ');
|
||||
WriteLE32(&header.fmt.header.Size, kFmtSubchunkSize);
|
||||
WriteLE16(&header.fmt.AudioFormat, format);
|
||||
WriteLE16(&header.fmt.NumChannels, num_channels);
|
||||
WriteLE32(&header.fmt.SampleRate, sample_rate);
|
||||
WriteLE32(&header.fmt.ByteRate, ByteRate(num_channels, sample_rate,
|
||||
bytes_per_sample));
|
||||
WriteLE16(&header.fmt.BlockAlign, BlockAlign(num_channels, bytes_per_sample));
|
||||
WriteLE16(&header.fmt.BitsPerSample, 8 * bytes_per_sample);
|
||||
|
||||
WriteFourCC(&header.data.header.ID, 'd', 'a', 't', 'a');
|
||||
WriteLE32(&header.data.header.Size, bytes_in_payload);
|
||||
|
||||
// Do an extra copy rather than writing everything to buf directly, since buf
|
||||
// might not be correctly aligned.
|
||||
memcpy(buf, &header, kWavHeaderSize);
|
||||
if (format == WavFormat::kWavFormatPcm) {
|
||||
WritePcmWavHeader(num_channels, sample_rate, bytes_per_sample, num_samples,
|
||||
buf, header_size);
|
||||
} else {
|
||||
RTC_CHECK_EQ(format, WavFormat::kWavFormatIeeeFloat);
|
||||
WriteIeeeFloatWavHeader(num_channels, sample_rate, bytes_per_sample,
|
||||
num_samples, buf, header_size);
|
||||
}
|
||||
}
|
||||
|
||||
bool ReadWavHeader(ReadableWav* readable,
|
||||
int* num_channels,
|
||||
bool ReadWavHeader(WavHeaderReader* readable,
|
||||
size_t* num_channels,
|
||||
int* sample_rate,
|
||||
WavFormat* format,
|
||||
int* bytes_per_sample,
|
||||
uint32_t* num_samples) {
|
||||
WavHeader header;
|
||||
if (readable->Read(&header, kWavHeaderSize - sizeof(header.data)) !=
|
||||
kWavHeaderSize - sizeof(header.data))
|
||||
return false;
|
||||
size_t* bytes_per_sample,
|
||||
size_t* num_samples,
|
||||
int64_t* data_start_pos) {
|
||||
// Read using the PCM header, even though it might be float Wav file
|
||||
auto header = rtc::MsanUninitialized<WavHeaderPcm>({});
|
||||
|
||||
const uint32_t fmt_size = ReadLE32(header.fmt.header.Size);
|
||||
if (fmt_size != kFmtSubchunkSize) {
|
||||
// There is an optional two-byte extension field permitted to be present
|
||||
// with PCM, but which must be zero.
|
||||
int16_t ext_size;
|
||||
if (kFmtSubchunkSize + sizeof(ext_size) != fmt_size)
|
||||
return false;
|
||||
if (readable->Read(&ext_size, sizeof(ext_size)) != sizeof(ext_size))
|
||||
return false;
|
||||
if (ext_size != 0)
|
||||
return false;
|
||||
}
|
||||
if (readable->Read(&header.data, sizeof(header.data)) != sizeof(header.data))
|
||||
// Read RIFF chunk.
|
||||
if (readable->Read(&header.riff, sizeof(header.riff)) != sizeof(header.riff))
|
||||
return false;
|
||||
|
||||
// Parse needed fields.
|
||||
*format = static_cast<WavFormat>(ReadLE16(header.fmt.AudioFormat));
|
||||
*num_channels = ReadLE16(header.fmt.NumChannels);
|
||||
*sample_rate = ReadLE32(header.fmt.SampleRate);
|
||||
*bytes_per_sample = ReadLE16(header.fmt.BitsPerSample) / 8;
|
||||
const uint32_t bytes_in_payload = ReadLE32(header.data.header.Size);
|
||||
if (*bytes_per_sample <= 0)
|
||||
return false;
|
||||
*num_samples = bytes_in_payload / *bytes_per_sample;
|
||||
|
||||
// Sanity check remaining fields.
|
||||
if (ReadFourCC(header.riff.header.ID) != "RIFF")
|
||||
return false;
|
||||
if (ReadFourCC(header.riff.Format) != "WAVE")
|
||||
return false;
|
||||
if (ReadFourCC(header.fmt.header.ID) != "fmt ")
|
||||
return false;
|
||||
if (ReadFourCC(header.data.header.ID) != "data")
|
||||
return false;
|
||||
|
||||
if (ReadLE32(header.riff.header.Size) < RiffChunkSize(bytes_in_payload))
|
||||
// Find "fmt " and "data" chunks. While the official Wave file specification
|
||||
// does not put requirements on the chunks order, it is uncommon to find the
|
||||
// "data" chunk before the "fmt " one. The code below fails if this is not the
|
||||
// case.
|
||||
if (!FindWaveChunk(&header.fmt.header, readable, "fmt ")) {
|
||||
RTC_LOG(LS_ERROR) << "Cannot find 'fmt ' chunk.";
|
||||
return false;
|
||||
if (ReadLE32(header.fmt.ByteRate) !=
|
||||
}
|
||||
if (!ReadFmtChunkData(&header.fmt, readable)) {
|
||||
RTC_LOG(LS_ERROR) << "Cannot read 'fmt ' chunk.";
|
||||
return false;
|
||||
}
|
||||
if (!FindWaveChunk(&header.data.header, readable, "data")) {
|
||||
RTC_LOG(LS_ERROR) << "Cannot find 'data' chunk.";
|
||||
return false;
|
||||
}
|
||||
|
||||
// Parse needed fields.
|
||||
*format = MapHeaderFieldToWavFormat(header.fmt.AudioFormat);
|
||||
*num_channels = header.fmt.NumChannels;
|
||||
*sample_rate = header.fmt.SampleRate;
|
||||
*bytes_per_sample = header.fmt.BitsPerSample / 8;
|
||||
const size_t bytes_in_payload = header.data.header.Size;
|
||||
if (*bytes_per_sample == 0)
|
||||
return false;
|
||||
*num_samples = bytes_in_payload / *bytes_per_sample;
|
||||
|
||||
const size_t header_size = *format == WavFormat::kWavFormatPcm
|
||||
? kPcmWavHeaderSize
|
||||
: kIeeeFloatWavHeaderSize;
|
||||
|
||||
if (header.riff.header.Size < RiffChunkSize(bytes_in_payload, header_size))
|
||||
return false;
|
||||
if (header.fmt.ByteRate !=
|
||||
ByteRate(*num_channels, *sample_rate, *bytes_per_sample))
|
||||
return false;
|
||||
if (ReadLE16(header.fmt.BlockAlign) !=
|
||||
BlockAlign(*num_channels, *bytes_per_sample))
|
||||
if (header.fmt.BlockAlign != BlockAlign(*num_channels, *bytes_per_sample))
|
||||
return false;
|
||||
|
||||
return CheckWavParameters(*num_channels, *sample_rate, *format,
|
||||
*bytes_per_sample, *num_samples);
|
||||
if (!CheckWavParameters(*num_channels, *sample_rate, *format,
|
||||
*bytes_per_sample, *num_samples)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*data_start_pos = readable->GetPosition();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
} // namespace webrtc
|
||||
|
Reference in New Issue
Block a user