diff options
Diffstat (limited to 'apps/codecs/demac/libdemac/parser.c')
-rw-r--r-- | apps/codecs/demac/libdemac/parser.c | 357 |
1 files changed, 357 insertions, 0 deletions
diff --git a/apps/codecs/demac/libdemac/parser.c b/apps/codecs/demac/libdemac/parser.c new file mode 100644 index 0000000000..bcb542dbb6 --- /dev/null +++ b/apps/codecs/demac/libdemac/parser.c | |||
@@ -0,0 +1,357 @@ | |||
1 | /* | ||
2 | |||
3 | libdemac - A Monkey's Audio decoder | ||
4 | |||
5 | $Id:$ | ||
6 | |||
7 | Copyright (C) Dave Chapman 2007 | ||
8 | |||
9 | This program is free software; you can redistribute it and/or modify | ||
10 | it under the terms of the GNU General Public License as published by | ||
11 | the Free Software Foundation; either version 2 of the License, or | ||
12 | (at your option) any later version. | ||
13 | |||
14 | This program is distributed in the hope that it will be useful, | ||
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | GNU General Public License for more details. | ||
18 | |||
19 | You should have received a copy of the GNU General Public License | ||
20 | along with this program; if not, write to the Free Software | ||
21 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA | ||
22 | |||
23 | */ | ||
24 | |||
25 | #include <inttypes.h> | ||
26 | #include <string.h> | ||
27 | #ifndef ROCKBOX | ||
28 | #include <stdio.h> | ||
29 | #include <stdlib.h> | ||
30 | #include <sys/types.h> | ||
31 | #include <sys/stat.h> | ||
32 | #include <fcntl.h> | ||
33 | #include <unistd.h> | ||
34 | #endif | ||
35 | |||
36 | #include "parser.h" | ||
37 | |||
38 | |||
39 | static inline int16_t get_int16(unsigned char* buf) | ||
40 | { | ||
41 | return(buf[0] | (buf[1] << 8)); | ||
42 | } | ||
43 | |||
44 | static inline uint16_t get_uint16(unsigned char* buf) | ||
45 | { | ||
46 | return(buf[0] | (buf[1] << 8)); | ||
47 | } | ||
48 | |||
49 | static inline uint32_t get_uint32(unsigned char* buf) | ||
50 | { | ||
51 | return(buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24)); | ||
52 | } | ||
53 | |||
54 | |||
55 | int ape_parseheaderbuf(unsigned char* buf, struct ape_ctx_t* ape_ctx) | ||
56 | { | ||
57 | unsigned char* header; | ||
58 | |||
59 | memset(ape_ctx,0,sizeof(struct ape_ctx_t)); | ||
60 | /* TODO: Skip any leading junk such as id3v2 tags */ | ||
61 | ape_ctx->junklength = 0; | ||
62 | |||
63 | memcpy(ape_ctx->magic, buf, 4); | ||
64 | if (memcmp(ape_ctx->magic,"MAC ",4)!=0) | ||
65 | { | ||
66 | return -1; | ||
67 | } | ||
68 | |||
69 | ape_ctx->fileversion = get_int16(buf + 4); | ||
70 | |||
71 | if (ape_ctx->fileversion >= 3980) | ||
72 | { | ||
73 | ape_ctx->padding1 = get_int16(buf + 6); | ||
74 | ape_ctx->descriptorlength = get_uint32(buf + 8); | ||
75 | ape_ctx->headerlength = get_uint32(buf + 12); | ||
76 | ape_ctx->seektablelength = get_uint32(buf + 16); | ||
77 | ape_ctx->wavheaderlength = get_uint32(buf + 20); | ||
78 | ape_ctx->audiodatalength = get_uint32(buf + 24); | ||
79 | ape_ctx->audiodatalength_high = get_uint32(buf + 28); | ||
80 | ape_ctx->wavtaillength = get_uint32(buf + 32); | ||
81 | memcpy(ape_ctx->md5, buf + 36, 16); | ||
82 | |||
83 | header = buf + ape_ctx->descriptorlength; | ||
84 | |||
85 | /* Read header data */ | ||
86 | ape_ctx->compressiontype = get_uint16(header + 0); | ||
87 | ape_ctx->formatflags = get_uint16(header + 2); | ||
88 | ape_ctx->blocksperframe = get_uint32(header + 4); | ||
89 | ape_ctx->finalframeblocks = get_uint32(header + 8); | ||
90 | ape_ctx->totalframes = get_uint32(header + 12); | ||
91 | ape_ctx->bps = get_uint16(header + 16); | ||
92 | ape_ctx->channels = get_uint16(header + 18); | ||
93 | ape_ctx->samplerate = get_uint32(header + 20); | ||
94 | |||
95 | ape_ctx->firstframe = ape_ctx->junklength + ape_ctx->descriptorlength + | ||
96 | ape_ctx->headerlength + ape_ctx->seektablelength + | ||
97 | ape_ctx->wavheaderlength; | ||
98 | } else { | ||
99 | ape_ctx->compressiontype = get_uint16(buf + 6); | ||
100 | ape_ctx->formatflags = get_uint16(buf + 8); | ||
101 | ape_ctx->channels = get_uint16(buf + 10); | ||
102 | ape_ctx->samplerate = get_uint32(buf + 14); | ||
103 | ape_ctx->wavheaderlength = get_uint32(buf + 18); | ||
104 | ape_ctx->totalframes = get_uint32(buf + 26); | ||
105 | ape_ctx->finalframeblocks = get_uint32(buf + 30); | ||
106 | } | ||
107 | |||
108 | ape_ctx->totalsamples = ape_ctx->finalframeblocks; | ||
109 | if (ape_ctx->totalframes > 1) | ||
110 | ape_ctx->totalsamples += ape_ctx->blocksperframe * (ape_ctx->totalframes-1); | ||
111 | |||
112 | /* TODO: Parse and store seektable */ | ||
113 | |||
114 | return 0; | ||
115 | } | ||
116 | |||
117 | |||
118 | #ifndef ROCKBOX | ||
119 | /* Helper functions */ | ||
120 | |||
121 | static int read_uint16(int fd, uint16_t* x) | ||
122 | { | ||
123 | unsigned char tmp[2]; | ||
124 | int n; | ||
125 | |||
126 | n = read(fd,tmp,2); | ||
127 | |||
128 | if (n != 2) | ||
129 | return -1; | ||
130 | |||
131 | *x = tmp[0] | (tmp[1] << 8); | ||
132 | |||
133 | return 0; | ||
134 | } | ||
135 | |||
136 | static int read_int16(int fd, int16_t* x) | ||
137 | { | ||
138 | return read_uint16(fd, (uint16_t*)x); | ||
139 | } | ||
140 | |||
141 | static int read_uint32(int fd, uint32_t* x) | ||
142 | { | ||
143 | unsigned char tmp[4]; | ||
144 | int n; | ||
145 | |||
146 | n = read(fd,tmp,4); | ||
147 | |||
148 | if (n != 4) | ||
149 | return -1; | ||
150 | |||
151 | *x = tmp[0] | (tmp[1] << 8) | (tmp[2] << 16) | (tmp[3] << 24); | ||
152 | |||
153 | return 0; | ||
154 | } | ||
155 | |||
156 | int ape_parseheader(int fd, struct ape_ctx_t* ape_ctx) | ||
157 | { | ||
158 | int i,n; | ||
159 | |||
160 | /* TODO: Skip any leading junk such as id3v2 tags */ | ||
161 | ape_ctx->junklength = 0; | ||
162 | |||
163 | lseek(fd,ape_ctx->junklength,SEEK_SET); | ||
164 | |||
165 | n = read(fd,&ape_ctx->magic,4); | ||
166 | if (n != 4) return -1; | ||
167 | |||
168 | if (memcmp(ape_ctx->magic,"MAC ",4)!=0) | ||
169 | { | ||
170 | return -1; | ||
171 | } | ||
172 | |||
173 | if (read_int16(fd,&ape_ctx->fileversion) < 0) | ||
174 | return -1; | ||
175 | |||
176 | if (ape_ctx->fileversion >= 3980) | ||
177 | { | ||
178 | if (read_int16(fd,&ape_ctx->padding1) < 0) | ||
179 | return -1; | ||
180 | if (read_uint32(fd,&ape_ctx->descriptorlength) < 0) | ||
181 | return -1; | ||
182 | if (read_uint32(fd,&ape_ctx->headerlength) < 0) | ||
183 | return -1; | ||
184 | if (read_uint32(fd,&ape_ctx->seektablelength) < 0) | ||
185 | return -1; | ||
186 | if (read_uint32(fd,&ape_ctx->wavheaderlength) < 0) | ||
187 | return -1; | ||
188 | if (read_uint32(fd,&ape_ctx->audiodatalength) < 0) | ||
189 | return -1; | ||
190 | if (read_uint32(fd,&ape_ctx->audiodatalength_high) < 0) | ||
191 | return -1; | ||
192 | if (read_uint32(fd,&ape_ctx->wavtaillength) < 0) | ||
193 | return -1; | ||
194 | if (read(fd,&ape_ctx->md5,16) != 16) | ||
195 | return -1; | ||
196 | |||
197 | /* Skip any unknown bytes at the end of the descriptor. This is for future | ||
198 | compatibility */ | ||
199 | if (ape_ctx->descriptorlength > 52) | ||
200 | lseek(fd,ape_ctx->descriptorlength - 52, SEEK_CUR); | ||
201 | |||
202 | /* Read header data */ | ||
203 | if (read_uint16(fd,&ape_ctx->compressiontype) < 0) | ||
204 | return -1; | ||
205 | if (read_uint16(fd,&ape_ctx->formatflags) < 0) | ||
206 | return -1; | ||
207 | if (read_uint32(fd,&ape_ctx->blocksperframe) < 0) | ||
208 | return -1; | ||
209 | if (read_uint32(fd,&ape_ctx->finalframeblocks) < 0) | ||
210 | return -1; | ||
211 | if (read_uint32(fd,&ape_ctx->totalframes) < 0) | ||
212 | return -1; | ||
213 | if (read_uint16(fd,&ape_ctx->bps) < 0) | ||
214 | return -1; | ||
215 | if (read_uint16(fd,&ape_ctx->channels) < 0) | ||
216 | return -1; | ||
217 | if (read_uint32(fd,&ape_ctx->samplerate) < 0) | ||
218 | return -1; | ||
219 | } else { | ||
220 | ape_ctx->descriptorlength = 0; | ||
221 | ape_ctx->headerlength = 32; | ||
222 | |||
223 | if (read_uint16(fd,&ape_ctx->compressiontype) < 0) | ||
224 | return -1; | ||
225 | if (read_uint16(fd,&ape_ctx->formatflags) < 0) | ||
226 | return -1; | ||
227 | if (read_uint16(fd,&ape_ctx->channels) < 0) | ||
228 | return -1; | ||
229 | if (read_uint32(fd,&ape_ctx->samplerate) < 0) | ||
230 | return -1; | ||
231 | if (read_uint32(fd,&ape_ctx->wavheaderlength) < 0) | ||
232 | return -1; | ||
233 | if (read_uint32(fd,&ape_ctx->wavtaillength) < 0) | ||
234 | return -1; | ||
235 | if (read_uint32(fd,&ape_ctx->totalframes) < 0) | ||
236 | return -1; | ||
237 | if (read_uint32(fd,&ape_ctx->finalframeblocks) < 0) | ||
238 | return -1; | ||
239 | |||
240 | if (ape_ctx->formatflags & MAC_FORMAT_FLAG_HAS_PEAK_LEVEL) | ||
241 | { | ||
242 | lseek(fd, 4, SEEK_CUR); /* Skip the peak level */ | ||
243 | ape_ctx->headerlength += 4; | ||
244 | } | ||
245 | |||
246 | if (ape_ctx->formatflags & MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS) | ||
247 | { | ||
248 | if (read_uint32(fd,&ape_ctx->seektablelength) < 0) | ||
249 | return -1; | ||
250 | ape_ctx->headerlength += 4; | ||
251 | ape_ctx->seektablelength *= sizeof(int32_t); | ||
252 | } else { | ||
253 | ape_ctx->seektablelength = ape_ctx->totalframes * sizeof(int32_t); | ||
254 | } | ||
255 | |||
256 | if (ape_ctx->formatflags & MAC_FORMAT_FLAG_8_BIT) | ||
257 | ape_ctx->bps = 8; | ||
258 | else if (ape_ctx->formatflags & MAC_FORMAT_FLAG_24_BIT) | ||
259 | ape_ctx->bps = 24; | ||
260 | else | ||
261 | ape_ctx->bps = 16; | ||
262 | |||
263 | if (ape_ctx->fileversion >= 3950) | ||
264 | ape_ctx->blocksperframe = 73728 * 4; | ||
265 | else if ((ape_ctx->fileversion >= 3900) || (ape_ctx->fileversion >= 3800 && ape_ctx->compressiontype >= 4000)) | ||
266 | ape_ctx->blocksperframe = 73728; | ||
267 | else | ||
268 | ape_ctx->blocksperframe = 9216; | ||
269 | |||
270 | /* Skip any stored wav header */ | ||
271 | if (!(ape_ctx->formatflags & MAC_FORMAT_FLAG_CREATE_WAV_HEADER)) | ||
272 | { | ||
273 | lseek(fd, ape_ctx->wavheaderlength, SEEK_CUR); | ||
274 | } | ||
275 | } | ||
276 | |||
277 | ape_ctx->totalsamples = ape_ctx->finalframeblocks; | ||
278 | if (ape_ctx->totalframes > 1) | ||
279 | ape_ctx->totalsamples += ape_ctx->blocksperframe * (ape_ctx->totalframes-1); | ||
280 | |||
281 | if (ape_ctx->seektablelength > 0) | ||
282 | { | ||
283 | ape_ctx->seektable = malloc(ape_ctx->seektablelength); | ||
284 | if (ape_ctx->seektable == NULL) | ||
285 | return -1; | ||
286 | for (i=0; i < ape_ctx->seektablelength / sizeof(uint32_t); i++) | ||
287 | { | ||
288 | if (read_uint32(fd,&ape_ctx->seektable[i]) < 0) | ||
289 | { | ||
290 | free(ape_ctx->seektable); | ||
291 | return -1; | ||
292 | } | ||
293 | } | ||
294 | } | ||
295 | |||
296 | ape_ctx->firstframe = ape_ctx->junklength + ape_ctx->descriptorlength + | ||
297 | ape_ctx->headerlength + ape_ctx->seektablelength + | ||
298 | ape_ctx->wavheaderlength; | ||
299 | |||
300 | return 0; | ||
301 | } | ||
302 | |||
303 | void ape_dumpinfo(struct ape_ctx_t* ape_ctx) | ||
304 | { | ||
305 | int i; | ||
306 | |||
307 | printf("Descriptor Block:\n\n"); | ||
308 | printf("magic = \"%c%c%c%c\"\n", | ||
309 | ape_ctx->magic[0],ape_ctx->magic[1], | ||
310 | ape_ctx->magic[2],ape_ctx->magic[3]); | ||
311 | printf("fileversion = %d\n",ape_ctx->fileversion); | ||
312 | printf("descriptorlength = %d\n",ape_ctx->descriptorlength); | ||
313 | printf("headerlength = %d\n",ape_ctx->headerlength); | ||
314 | printf("seektablelength = %d\n",ape_ctx->seektablelength); | ||
315 | printf("wavheaderlength = %d\n",ape_ctx->wavheaderlength); | ||
316 | printf("audiodatalength = %d\n",ape_ctx->audiodatalength); | ||
317 | printf("audiodatalength_high = %d\n",ape_ctx->audiodatalength_high); | ||
318 | printf("wavtaillength = %d\n",ape_ctx->wavtaillength); | ||
319 | printf("md5 = "); | ||
320 | for (i = 0; i < 16; i++) | ||
321 | printf("%02x",ape_ctx->md5[i]); | ||
322 | printf("\n"); | ||
323 | |||
324 | printf("\nHeader Block:\n\n"); | ||
325 | |||
326 | printf("compressiontype = %d\n",ape_ctx->compressiontype); | ||
327 | printf("formatflags = %d\n",ape_ctx->formatflags); | ||
328 | printf("blocksperframe = %d\n",ape_ctx->blocksperframe); | ||
329 | printf("finalframeblocks = %d\n",ape_ctx->finalframeblocks); | ||
330 | printf("totalframes = %d\n",ape_ctx->totalframes); | ||
331 | printf("bps = %d\n",ape_ctx->bps); | ||
332 | printf("channels = %d\n",ape_ctx->channels); | ||
333 | printf("samplerate = %d\n",ape_ctx->samplerate); | ||
334 | |||
335 | printf("\nSeektable\n\n"); | ||
336 | if ((ape_ctx->seektablelength / sizeof(uint32_t)) != ape_ctx->totalframes) | ||
337 | { | ||
338 | printf("No seektable\n"); | ||
339 | } | ||
340 | else | ||
341 | { | ||
342 | for ( i = 0; i < ape_ctx->seektablelength / sizeof(uint32_t) ; i++) | ||
343 | { | ||
344 | if (i < ape_ctx->totalframes-1) { | ||
345 | printf("%8d %d (%d bytes)\n",i,ape_ctx->seektable[i],ape_ctx->seektable[i+1]-ape_ctx->seektable[i]); | ||
346 | } else { | ||
347 | printf("%8d %d\n",i,ape_ctx->seektable[i]); | ||
348 | } | ||
349 | } | ||
350 | } | ||
351 | printf("\nCalculated information:\n\n"); | ||
352 | printf("junklength = %d\n",ape_ctx->junklength); | ||
353 | printf("firstframe = %d\n",ape_ctx->firstframe); | ||
354 | printf("totalsamples = %d\n",ape_ctx->totalsamples); | ||
355 | } | ||
356 | |||
357 | #endif /* !ROCKBOX */ | ||