forked from Green-Sky/tomato
update sdl Merge commit '4d48f9d23713d94b861da7b5d41baf2a41334994'
This commit is contained in:
76
external/sdl/SDL/src/audio/SDL_wave.c
vendored
76
external/sdl/SDL/src/audio/SDL_wave.c
vendored
@@ -1241,7 +1241,7 @@ static int LAW_Decode(WaveFile *file, Uint8 **audio_buf, Uint32 *audio_len)
|
||||
|
||||
dst = (Sint16 *)src;
|
||||
|
||||
/* Work backwards, since we're expanding in-place. SDL_AudioSpec.format will
|
||||
/* Work backwards, since we're expanding in-place. `format` will
|
||||
* inform the caller about the byte order.
|
||||
*/
|
||||
i = sample_count;
|
||||
@@ -1553,7 +1553,7 @@ static int WaveReadPartialChunkData(SDL_RWops *src, WaveChunk *chunk, size_t len
|
||||
return -2;
|
||||
}
|
||||
|
||||
chunk->size = (size_t) SDL_RWread(src, chunk->data, length);
|
||||
chunk->size = SDL_RWread(src, chunk->data, length);
|
||||
if (chunk->size != length) {
|
||||
/* Expected to be handled by the caller. */
|
||||
}
|
||||
@@ -1614,16 +1614,20 @@ static int WaveReadFormat(WaveFile *file)
|
||||
return SDL_OutOfMemory();
|
||||
}
|
||||
|
||||
format->formattag = SDL_ReadLE16(fmtsrc);
|
||||
if (!SDL_ReadU16LE(fmtsrc, &format->formattag) ||
|
||||
!SDL_ReadU16LE(fmtsrc, &format->channels) ||
|
||||
!SDL_ReadU32LE(fmtsrc, &format->frequency) ||
|
||||
!SDL_ReadU32LE(fmtsrc, &format->byterate) ||
|
||||
!SDL_ReadU16LE(fmtsrc, &format->blockalign)) {
|
||||
return -1;
|
||||
}
|
||||
format->encoding = format->formattag;
|
||||
format->channels = SDL_ReadLE16(fmtsrc);
|
||||
format->frequency = SDL_ReadLE32(fmtsrc);
|
||||
format->byterate = SDL_ReadLE32(fmtsrc);
|
||||
format->blockalign = SDL_ReadLE16(fmtsrc);
|
||||
|
||||
/* This is PCM specific in the first version of the specification. */
|
||||
if (fmtlen >= 16) {
|
||||
format->bitspersample = SDL_ReadLE16(fmtsrc);
|
||||
if (!SDL_ReadU16LE(fmtsrc, &format->bitspersample)) {
|
||||
return -1;
|
||||
}
|
||||
} else if (format->encoding == PCM_CODE) {
|
||||
SDL_RWclose(fmtsrc);
|
||||
return SDL_SetError("Missing wBitsPerSample field in WAVE fmt chunk");
|
||||
@@ -1631,7 +1635,9 @@ static int WaveReadFormat(WaveFile *file)
|
||||
|
||||
/* The earlier versions also don't have this field. */
|
||||
if (fmtlen >= 18) {
|
||||
format->extsize = SDL_ReadLE16(fmtsrc);
|
||||
if (!SDL_ReadU16LE(fmtsrc, &format->extsize)) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (format->formattag == EXTENSIBLE_CODE) {
|
||||
@@ -1647,10 +1653,11 @@ static int WaveReadFormat(WaveFile *file)
|
||||
return SDL_SetError("Extensible WAVE header too small");
|
||||
}
|
||||
|
||||
format->validsamplebits = SDL_ReadLE16(fmtsrc);
|
||||
if (!SDL_ReadU16LE(fmtsrc, &format->validsamplebits) ||
|
||||
!SDL_ReadU32LE(fmtsrc, &format->channelmask) ||
|
||||
SDL_RWread(fmtsrc, format->subformat, 16) != 16) {
|
||||
}
|
||||
format->samplesperblock = format->validsamplebits;
|
||||
format->channelmask = SDL_ReadLE32(fmtsrc);
|
||||
SDL_RWread(fmtsrc, format->subformat, 16);
|
||||
format->encoding = WaveGetFormatGUIDEncoding(format);
|
||||
}
|
||||
|
||||
@@ -1667,15 +1674,11 @@ static int WaveCheckFormat(WaveFile *file, size_t datalength)
|
||||
|
||||
if (format->channels == 0) {
|
||||
return SDL_SetError("Invalid number of channels");
|
||||
} else if (format->channels > 255) {
|
||||
/* Limit given by SDL_AudioSpec.channels. */
|
||||
return SDL_SetError("Number of channels exceeds limit of 255");
|
||||
}
|
||||
|
||||
if (format->frequency == 0) {
|
||||
return SDL_SetError("Invalid sample rate");
|
||||
} else if (format->frequency > INT_MAX) {
|
||||
/* Limit given by SDL_AudioSpec.freq. */
|
||||
return SDL_SetError("Sample rate exceeds limit of %d", INT_MAX);
|
||||
}
|
||||
|
||||
@@ -1806,9 +1809,9 @@ static int WaveLoad(SDL_RWops *src, WaveFile *file, SDL_AudioSpec *spec, Uint8 *
|
||||
if (RIFFchunk.fourcc == RIFF) {
|
||||
Uint32 formtype;
|
||||
/* Read the form type. "WAVE" expected. */
|
||||
if (SDL_RWread(src, &formtype, sizeof(Uint32)) != sizeof(Uint32)) {
|
||||
if (!SDL_ReadU32LE(src, &formtype)) {
|
||||
return SDL_SetError("Could not read RIFF form type");
|
||||
} else if (SDL_SwapLE32(formtype) != WAVE) {
|
||||
} else if (formtype != WAVE) {
|
||||
return SDL_SetError("RIFF form type is not WAVE (not a Waveform file)");
|
||||
}
|
||||
} else if (RIFFchunk.fourcc == WAVE) {
|
||||
@@ -1895,10 +1898,8 @@ static int WaveLoad(SDL_RWops *src, WaveFile *file, SDL_AudioSpec *spec, Uint8 *
|
||||
} else {
|
||||
/* Let's use src directly, it's just too convenient. */
|
||||
Sint64 position = SDL_RWseek(src, chunk->position, SDL_RW_SEEK_SET);
|
||||
Uint32 samplelength;
|
||||
if (position == chunk->position && SDL_RWread(src, &samplelength, sizeof(Uint32)) == sizeof(Uint32)) {
|
||||
if (position == chunk->position && SDL_ReadU32LE(src, &file->fact.samplelength)) {
|
||||
file->fact.status = 1;
|
||||
file->fact.samplelength = SDL_SwapLE32(samplelength);
|
||||
} else {
|
||||
file->fact.status = -1;
|
||||
}
|
||||
@@ -1941,7 +1942,7 @@ static int WaveLoad(SDL_RWops *src, WaveFile *file, SDL_AudioSpec *spec, Uint8 *
|
||||
Uint64 position = (Uint64)chunk->position + chunk->length - 1;
|
||||
if (position > SDL_MAX_SINT64 || SDL_RWseek(src, (Sint64)position, SDL_RW_SEEK_SET) != (Sint64)position) {
|
||||
return SDL_SetError("Could not seek to WAVE chunk data");
|
||||
} else if (SDL_RWread(src, &tmp, 1) != 1) {
|
||||
} else if (!SDL_ReadU8(src, &tmp)) {
|
||||
return SDL_SetError("RIFF size truncates chunk");
|
||||
}
|
||||
}
|
||||
@@ -2025,13 +2026,12 @@ static int WaveLoad(SDL_RWops *src, WaveFile *file, SDL_AudioSpec *spec, Uint8 *
|
||||
break;
|
||||
}
|
||||
|
||||
/* Setting up the SDL_AudioSpec. All unsupported formats were filtered out
|
||||
/* Setting up the specs. All unsupported formats were filtered out
|
||||
* by checks earlier in this function.
|
||||
*/
|
||||
SDL_zerop(spec);
|
||||
spec->freq = format->frequency;
|
||||
spec->channels = (Uint8)format->channels;
|
||||
spec->samples = 4096; /* Good default buffer size */
|
||||
spec->format = 0;
|
||||
|
||||
switch (format->encoding) {
|
||||
case MS_ADPCM_CODE:
|
||||
@@ -2061,10 +2061,10 @@ static int WaveLoad(SDL_RWops *src, WaveFile *file, SDL_AudioSpec *spec, Uint8 *
|
||||
return SDL_SetError("Unexpected %u-bit PCM data format", (unsigned int)format->bitspersample);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return SDL_SetError("Unexpected data format");
|
||||
}
|
||||
|
||||
spec->silence = SDL_GetSilenceValueForFormat(spec->format);
|
||||
|
||||
/* Report the end position back to the cleanup code. */
|
||||
if (RIFFlengthknown) {
|
||||
chunk->position = RIFFend;
|
||||
@@ -2075,17 +2075,14 @@ static int WaveLoad(SDL_RWops *src, WaveFile *file, SDL_AudioSpec *spec, Uint8 *
|
||||
return 0;
|
||||
}
|
||||
|
||||
SDL_AudioSpec *SDL_LoadWAV_RW(SDL_RWops *src, SDL_bool freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
|
||||
int SDL_LoadWAV_RW(SDL_RWops *src, SDL_bool freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
|
||||
{
|
||||
int result = -1;
|
||||
WaveFile file;
|
||||
|
||||
SDL_zero(file);
|
||||
|
||||
/* Make sure we are passed a valid data source */
|
||||
if (src == NULL) {
|
||||
/* Error may come from RWops. */
|
||||
goto done;
|
||||
goto done; /* Error may come from RWops. */
|
||||
} else if (spec == NULL) {
|
||||
SDL_InvalidParamError("spec");
|
||||
goto done;
|
||||
@@ -2100,6 +2097,7 @@ SDL_AudioSpec *SDL_LoadWAV_RW(SDL_RWops *src, SDL_bool freesrc, SDL_AudioSpec *s
|
||||
*audio_buf = NULL;
|
||||
*audio_len = 0;
|
||||
|
||||
SDL_zero(file);
|
||||
file.riffhint = WaveGetRiffSizeHint();
|
||||
file.trunchint = WaveGetTruncationHint();
|
||||
file.facthint = WaveGetFactChunkHint();
|
||||
@@ -2107,7 +2105,6 @@ SDL_AudioSpec *SDL_LoadWAV_RW(SDL_RWops *src, SDL_bool freesrc, SDL_AudioSpec *s
|
||||
result = WaveLoad(src, &file, spec, audio_buf, audio_len);
|
||||
if (result < 0) {
|
||||
SDL_free(*audio_buf);
|
||||
spec = NULL;
|
||||
audio_buf = NULL;
|
||||
audio_len = 0;
|
||||
}
|
||||
@@ -2118,14 +2115,15 @@ SDL_AudioSpec *SDL_LoadWAV_RW(SDL_RWops *src, SDL_bool freesrc, SDL_AudioSpec *s
|
||||
}
|
||||
WaveFreeChunkData(&file.chunk);
|
||||
SDL_free(file.decoderdata);
|
||||
|
||||
done:
|
||||
if (freesrc && src) {
|
||||
SDL_RWclose(src);
|
||||
}
|
||||
if (result == 0) {
|
||||
return spec;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int SDL_LoadWAV(const char *path, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
|
||||
{
|
||||
return SDL_LoadWAV_RW(SDL_RWFromFile(path, "rb"), 1, spec, audio_buf, audio_len);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user