summaryrefslogtreecommitdiff
path: root/DevIL/src-IL/src/il_mp3.cpp
blob: d75711b927426538044bcb8c839efeff98dc8a0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//-----------------------------------------------------------------------------
//
// ImageLib Sources
// Copyright (C) 2000-2009 by Denton Woods
// Last modified: 03/05/2009
//
// Filename: src-IL/src/il_mp3.c
//
// MimeType: Reads from an MPEG-1 Audio Layer 3 (.mp3) file.
//
//-----------------------------------------------------------------------------

#include "il_internal.h"
#ifndef IL_NO_MP3

typedef struct MP3HEAD
{
	char	Signature[3];
	ILubyte	VersionMajor;
	ILubyte	VersionMinor;
	ILubyte	Flags;
	ILuint	Length;
} MP3HEAD;

#define MP3_NONE 0
#define MP3_JPG  1
#define MP3_PNG  2

ILboolean iLoadMp3Internal(void);
ILboolean iIsValidMp3(void);
ILboolean iCheckMp3(MP3HEAD *Header);
ILboolean iLoadJpegInternal(void);
ILboolean iLoadPngInternal(void);


//! Checks if the file specified in FileName is a valid MP3 file.
ILboolean ilIsValidMp3(ILconst_string FileName)
{
	ILHANDLE	Mp3File;
	ILboolean	bMp3 = IL_FALSE;
	
	if (!iCheckExtension(FileName, IL_TEXT("mp3"))) {
		ilSetError(IL_INVALID_EXTENSION);
		return bMp3;
	}
	
	Mp3File = iopenr(FileName);
	if (Mp3File == NULL) {
		ilSetError(IL_COULD_NOT_OPEN_FILE);
		return bMp3;
	}
	
	bMp3 = ilIsValidMp3F(Mp3File);
	icloser(Mp3File);
	
	return bMp3;
}


//! Checks if the ILHANDLE contains a valid MP3 file at the current position.
ILboolean ilIsValidMp3F(ILHANDLE File)
{
	ILuint		FirstPos;
	ILboolean	bRet;
	
	iSetInputFile(File);
	FirstPos = itell();
	bRet = iIsValidMp3();
	iseek(FirstPos, IL_SEEK_SET);
	
	return bRet;
}


//! Checks if Lump is a valid MP3 lump.
ILboolean ilIsValidMp3L(const void *Lump, ILuint Size)
{
	iSetInputLump(Lump, Size);
	return iIsValidMp3();
}


ILuint GetSynchInt()
{
	ILuint SynchInt;

	SynchInt = GetBigUInt();

	SynchInt = ((SynchInt & 0x7F000000) >> 3) | ((SynchInt & 0x7F0000) >> 2)
				| ((SynchInt & 0x7F00) >> 1) | (SynchInt & 0x7F);

	return SynchInt;
}


// Internal function used to get the MP3 header from the current file.
ILboolean iGetMp3Head(MP3HEAD *Header)
{
	if (iread(Header->Signature, 3, 1) != 1)
		return IL_FALSE;
	Header->VersionMajor = igetc();
	Header->VersionMinor = igetc();
	Header->Flags = igetc();
	Header->Length = GetSynchInt();

	return IL_TRUE;
}


// Internal function to get the header and check it.
ILboolean iIsValidMp3(void)
{
	MP3HEAD		Header;
	ILuint		Pos = itell();

	if (!iGetMp3Head(&Header))
		return IL_FALSE;
	// The length of the header varies, so we just go back to the original position.
	iseek(Pos, IL_SEEK_CUR);

	return iCheckMp3(&Header);
}


// Internal function used to check if the HEADER is a valid MP3 header.
ILboolean iCheckMp3(MP3HEAD *Header)
{
	if (strncmp(Header->Signature, "ID3", 3))
		return IL_FALSE;
	if (Header->VersionMajor != 3 && Header->VersionMinor != 4)
		return IL_FALSE;

	return IL_TRUE;
}


ILuint iFindMp3Pic(MP3HEAD *Header)
{
	char	ID[4];
	ILuint	FrameSize;
	ILubyte	TextEncoding;
	ILubyte	MimeType[65], Description[65];
	ILubyte	PicType;
	ILuint	i;
	ILuint	Type = MP3_NONE;

	do {
		if (iread(ID, 4, 1) != 1)
			return MP3_NONE;
		if (Header->VersionMajor == 3)
			FrameSize = GetBigUInt();
		else
			FrameSize = GetSynchInt();

		GetBigUShort();  // Skip the flags.

		//@TODO: Support multiple APIC entries in an mp3 file.
		if (!strncmp(ID, "APIC", 4)) {
			//@TODO: Use TextEncoding properly - UTF16 strings starting with FFFE or FEFF.
			TextEncoding = igetc();
			// Get the MimeType (read until we hit 0).
			for (i = 0; i < 65; i++) {
				MimeType[i] = igetc();
				if (MimeType[i] == 0)
					break;
			}
			// The MimeType must be terminated by 0 in the file by the specs.
			if (MimeType[i] != 0)
				return MP3_NONE;
			if (!strcmp(MimeType, "image/jpeg"))
				Type = MP3_JPG;
			else if (!strcmp(MimeType, "image/png"))
				Type = MP3_PNG;
			else
				Type = MP3_NONE;

			PicType = igetc();  // Whether this is a cover, band logo, etc.

			// Skip the description.
			for (i = 0; i < 65; i++) {
				Description[i] = igetc();
				if (Description[i] == 0)
					break;
			}
			if (Description[i] != 0)
				return MP3_NONE;
			return Type;
		}
		else {
			iseek(FrameSize, IL_SEEK_CUR);
		}

		//if (!strncmp(MimeType, "
	} while (!ieof() && itell() < Header->Length);

	return Type;
}


//! Reads a MP3 file
ILboolean ilLoadMp3(ILconst_string FileName)
{
	ILHANDLE	Mp3File;
	ILboolean	bMp3 = IL_FALSE;

	Mp3File = iopenr(FileName);
	if (Mp3File == NULL) {
		ilSetError(IL_COULD_NOT_OPEN_FILE);
		return bMp3;
	}

	bMp3 = ilLoadMp3F(Mp3File);
	icloser(Mp3File);

	return bMp3;
}


//! Reads an already-opened MP3 file
ILboolean ilLoadMp3F(ILHANDLE File)
{
	ILuint		FirstPos;
	ILboolean	bRet;

	iSetInputFile(File);
	FirstPos = itell();
	bRet = iLoadMp3Internal();
	iseek(FirstPos, IL_SEEK_SET);

	return bRet;
}


//! Reads from a memory "lump" that contains a MP3
ILboolean ilLoadMp3L(const void *Lump, ILuint Size)
{
	iSetInputLump(Lump, Size);
	return iLoadMp3Internal();
}


// Internal function used to load the MP3.
ILboolean iLoadMp3Internal(void)
{
	MP3HEAD	Header;
	ILuint	Type;

	if (iCurImage == NULL) {
		ilSetError(IL_ILLEGAL_OPERATION);
		return IL_FALSE;
	}

	if (!iGetMp3Head(&Header))
		return IL_FALSE;
	if (!iCheckMp3(&Header))
		return IL_FALSE;
	Type = iFindMp3Pic(&Header);
	
	switch (Type)
	{
#ifndef IL_NO_JPG
		case MP3_JPG:
			return iLoadJpegInternal();
#endif//IL_NO_JPG

#ifndef IL_NO_PNG
		case MP3_PNG:
			return iLoadPngInternal();
#endif//IL_NO_PNG

		// Either a picture was not found, or the MIME type was not recognized.
		default:
			ilSetError(IL_INVALID_FILE_HEADER);
	}

	return IL_FALSE;
}

#endif//IL_NO_MP3