1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include "../SDL_internal.h"
22
23/* RIFF WAVE files are little-endian */
24
25/*******************************************/
26/* Define values for Microsoft WAVE format */
27/*******************************************/
28/* FOURCC */
29#define RIFF 0x46464952 /* "RIFF" */
30#define WAVE 0x45564157 /* "WAVE" */
31#define FACT 0x74636166 /* "fact" */
32#define LIST 0x5453494c /* "LIST" */
33#define BEXT 0x74786562 /* "bext" */
34#define JUNK 0x4B4E554A /* "JUNK" */
35#define FMT 0x20746D66 /* "fmt " */
36#define DATA 0x61746164 /* "data" */
37/* Format tags */
38#define UNKNOWN_CODE 0x0000
39#define PCM_CODE 0x0001
40#define MS_ADPCM_CODE 0x0002
41#define IEEE_FLOAT_CODE 0x0003
42#define ALAW_CODE 0x0006
43#define MULAW_CODE 0x0007
44#define IMA_ADPCM_CODE 0x0011
45#define MPEG_CODE 0x0050
46#define MPEGLAYER3_CODE 0x0055
47#define EXTENSIBLE_CODE 0xFFFE
48
49/* Stores the WAVE format information. */
50typedef struct WaveFormat
51{
52 Uint16 formattag; /* Raw value of the first field in the fmt chunk data. */
53 Uint16 encoding; /* Actual encoding, possibly from the extensible header. */
54 Uint16 channels; /* Number of channels. */
55 Uint32 frequency; /* Sampling rate in Hz. */
56 Uint32 byterate; /* Average bytes per second. */
57 Uint16 blockalign; /* Bytes per block. */
58 Uint16 bitspersample; /* Currently supported are 8, 16, 24, 32, and 4 for ADPCM. */
59
60 /* Extra information size. Number of extra bytes starting at byte 18 in the
61 * fmt chunk data. This is at least 22 for the extensible header.
62 */
63 Uint16 extsize;
64
65 /* Extensible WAVE header fields */
66 Uint16 validsamplebits;
67 Uint32 samplesperblock; /* For compressed formats. Can be zero. Actually 16 bits in the header. */
68 Uint32 channelmask;
69 Uint8 subformat[16]; /* A format GUID. */
70} WaveFormat;
71
72/* Stores information on the fact chunk. */
73typedef struct WaveFact {
74 /* Represents the state of the fact chunk in the WAVE file.
75 * Set to -1 if the fact chunk is invalid.
76 * Set to 0 if the fact chunk is not present
77 * Set to 1 if the fact chunk is present and valid.
78 * Set to 2 if samplelength is going to be used as the number of sample frames.
79 */
80 Sint32 status;
81
82 /* Version 1 of the RIFF specification calls the field in the fact chunk
83 * dwFileSize. The Standards Update then calls it dwSampleLength and specifies
84 * that it is 'the length of the data in samples'. WAVE files from Windows
85 * with this chunk have it set to the samples per channel (sample frames).
86 * This is useful to truncate compressed audio to a specific sample count
87 * because a compressed block is usually decoded to a fixed number of
88 * sample frames.
89 */
90 Uint32 samplelength; /* Raw sample length value from the fact chunk. */
91} WaveFact;
92
93/* Generic struct for the chunks in the WAVE file. */
94typedef struct WaveChunk
95{
96 Uint32 fourcc; /* FOURCC of the chunk. */
97 Uint32 length; /* Size of the chunk data. */
98 Sint64 position; /* Position of the data in the stream. */
99 Uint8 *data; /* When allocated, this points to the chunk data. length is used for the malloc size. */
100 size_t size; /* Number of bytes in data that could be read from the stream. Can be smaller than length. */
101} WaveChunk;
102
103/* Controls how the size of the RIFF chunk affects the loading of a WAVE file. */
104typedef enum WaveRiffSizeHint {
105 RiffSizeNoHint,
106 RiffSizeForce,
107 RiffSizeIgnoreZero,
108 RiffSizeIgnore,
109 RiffSizeMaximum
110} WaveRiffSizeHint;
111
112/* Controls how a truncated WAVE file is handled. */
113typedef enum WaveTruncationHint {
114 TruncNoHint,
115 TruncVeryStrict,
116 TruncStrict,
117 TruncDropFrame,
118 TruncDropBlock
119} WaveTruncationHint;
120
121/* Controls how the fact chunk affects the loading of a WAVE file. */
122typedef enum WaveFactChunkHint {
123 FactNoHint,
124 FactTruncate,
125 FactStrict,
126 FactIgnoreZero,
127 FactIgnore
128} WaveFactChunkHint;
129
130typedef struct WaveFile
131{
132 WaveChunk chunk;
133 WaveFormat format;
134 WaveFact fact;
135
136 /* Number of sample frames that will be decoded. Calculated either with the
137 * size of the data chunk or, if the appropriate hint is enabled, with the
138 * sample length value from the fact chunk.
139 */
140 Sint64 sampleframes;
141
142 void *decoderdata; /* Some decoders require extra data for a state. */
143
144 WaveRiffSizeHint riffhint;
145 WaveTruncationHint trunchint;
146 WaveFactChunkHint facthint;
147} WaveFile;
148
149/* vi: set ts=4 sw=4 expandtab: */
150