diff options
author | Sean Bartell <wingedtachikoma@gmail.com> | 2011-06-25 21:32:25 -0400 |
---|---|---|
committer | Nils Wallménius <nils@rockbox.org> | 2012-04-25 22:13:20 +0200 |
commit | f40bfc9267b13b54e6379dfe7539447662879d24 (patch) | |
tree | 9b20069d5e62809ff434061ad730096836f916f2 /lib/rbcodec/codecs/demac/libdemac/parser.c | |
parent | a0009907de7a0107d49040d8a180f140e2eff299 (diff) | |
download | rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.tar.gz rockbox-f40bfc9267b13b54e6379dfe7539447662879d24.zip |
Add codecs to librbcodec.
Change-Id: Id7f4717d51ed02d67cb9f9cb3c0ada4a81843f97
Reviewed-on: http://gerrit.rockbox.org/137
Reviewed-by: Nils Wallménius <nils@rockbox.org>
Tested-by: Nils Wallménius <nils@rockbox.org>
Diffstat (limited to 'lib/rbcodec/codecs/demac/libdemac/parser.c')
-rw-r--r-- | lib/rbcodec/codecs/demac/libdemac/parser.c | 402 |
1 files changed, 402 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/demac/libdemac/parser.c b/lib/rbcodec/codecs/demac/libdemac/parser.c new file mode 100644 index 0000000000..2af4a292b8 --- /dev/null +++ b/lib/rbcodec/codecs/demac/libdemac/parser.c | |||
@@ -0,0 +1,402 @@ | |||
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 "inttypes.h" | ||
31 | #include <sys/stat.h> | ||
32 | #include <fcntl.h> | ||
33 | #include <unistd.h> | ||
34 | #endif | ||
35 | |||
36 | #include "parser.h" | ||
37 | |||
38 | #ifdef APE_MAX | ||
39 | #undef APE_MAX | ||
40 | #endif | ||
41 | #define APE_MAX(a,b) ((a)>(b)?(a):(b)) | ||
42 | |||
43 | |||
44 | static inline int16_t get_int16(unsigned char* buf) | ||
45 | { | ||
46 | return(buf[0] | (buf[1] << 8)); | ||
47 | } | ||
48 | |||
49 | static inline uint16_t get_uint16(unsigned char* buf) | ||
50 | { | ||
51 | return(buf[0] | (buf[1] << 8)); | ||
52 | } | ||
53 | |||
54 | static inline uint32_t get_uint32(unsigned char* buf) | ||
55 | { | ||
56 | return(buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24)); | ||
57 | } | ||
58 | |||
59 | |||
60 | int ape_parseheaderbuf(unsigned char* buf, struct ape_ctx_t* ape_ctx) | ||
61 | { | ||
62 | unsigned char* header; | ||
63 | |||
64 | memset(ape_ctx,0,sizeof(struct ape_ctx_t)); | ||
65 | /* TODO: Skip any leading junk such as id3v2 tags */ | ||
66 | ape_ctx->junklength = 0; | ||
67 | |||
68 | memcpy(ape_ctx->magic, buf, 4); | ||
69 | if (memcmp(ape_ctx->magic,"MAC ",4)!=0) | ||
70 | { | ||
71 | return -1; | ||
72 | } | ||
73 | |||
74 | ape_ctx->fileversion = get_int16(buf + 4); | ||
75 | |||
76 | if (ape_ctx->fileversion >= 3980) | ||
77 | { | ||
78 | ape_ctx->padding1 = get_int16(buf + 6); | ||
79 | ape_ctx->descriptorlength = get_uint32(buf + 8); | ||
80 | ape_ctx->headerlength = get_uint32(buf + 12); | ||
81 | ape_ctx->seektablelength = get_uint32(buf + 16); | ||
82 | ape_ctx->wavheaderlength = get_uint32(buf + 20); | ||
83 | ape_ctx->audiodatalength = get_uint32(buf + 24); | ||
84 | ape_ctx->audiodatalength_high = get_uint32(buf + 28); | ||
85 | ape_ctx->wavtaillength = get_uint32(buf + 32); | ||
86 | memcpy(ape_ctx->md5, buf + 36, 16); | ||
87 | |||
88 | header = buf + ape_ctx->descriptorlength; | ||
89 | |||
90 | /* Read header data */ | ||
91 | ape_ctx->compressiontype = get_uint16(header + 0); | ||
92 | ape_ctx->formatflags = get_uint16(header + 2); | ||
93 | ape_ctx->blocksperframe = get_uint32(header + 4); | ||
94 | ape_ctx->finalframeblocks = get_uint32(header + 8); | ||
95 | ape_ctx->totalframes = get_uint32(header + 12); | ||
96 | ape_ctx->bps = get_uint16(header + 16); | ||
97 | ape_ctx->channels = get_uint16(header + 18); | ||
98 | ape_ctx->samplerate = get_uint32(header + 20); | ||
99 | |||
100 | ape_ctx->seektablefilepos = ape_ctx->junklength + | ||
101 | ape_ctx->descriptorlength + | ||
102 | ape_ctx->headerlength; | ||
103 | |||
104 | ape_ctx->firstframe = ape_ctx->junklength + ape_ctx->descriptorlength + | ||
105 | ape_ctx->headerlength + ape_ctx->seektablelength + | ||
106 | ape_ctx->wavheaderlength; | ||
107 | } else { | ||
108 | ape_ctx->headerlength = 32; | ||
109 | ape_ctx->compressiontype = get_uint16(buf + 6); | ||
110 | ape_ctx->formatflags = get_uint16(buf + 8); | ||
111 | ape_ctx->channels = get_uint16(buf + 10); | ||
112 | ape_ctx->samplerate = get_uint32(buf + 12); | ||
113 | ape_ctx->wavheaderlength = get_uint32(buf + 16); | ||
114 | ape_ctx->totalframes = get_uint32(buf + 24); | ||
115 | ape_ctx->finalframeblocks = get_uint32(buf + 28); | ||
116 | |||
117 | if (ape_ctx->formatflags & MAC_FORMAT_FLAG_HAS_PEAK_LEVEL) | ||
118 | { | ||
119 | ape_ctx->headerlength += 4; | ||
120 | } | ||
121 | |||
122 | if (ape_ctx->formatflags & MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS) | ||
123 | { | ||
124 | ape_ctx->seektablelength = get_uint32(buf + ape_ctx->headerlength); | ||
125 | ape_ctx->seektablelength *= sizeof(int32_t); | ||
126 | ape_ctx->headerlength += 4; | ||
127 | } else { | ||
128 | ape_ctx->seektablelength = ape_ctx->totalframes * sizeof(int32_t); | ||
129 | } | ||
130 | |||
131 | if (ape_ctx->formatflags & MAC_FORMAT_FLAG_8_BIT) | ||
132 | ape_ctx->bps = 8; | ||
133 | else if (ape_ctx->formatflags & MAC_FORMAT_FLAG_24_BIT) | ||
134 | ape_ctx->bps = 24; | ||
135 | else | ||
136 | ape_ctx->bps = 16; | ||
137 | |||
138 | if (ape_ctx->fileversion >= 3950) | ||
139 | ape_ctx->blocksperframe = 73728 * 4; | ||
140 | else if ((ape_ctx->fileversion >= 3900) || (ape_ctx->fileversion >= 3800 && ape_ctx->compressiontype >= 4000)) | ||
141 | ape_ctx->blocksperframe = 73728; | ||
142 | else | ||
143 | ape_ctx->blocksperframe = 9216; | ||
144 | |||
145 | ape_ctx->seektablefilepos = ape_ctx->junklength + ape_ctx->headerlength + | ||
146 | ape_ctx->wavheaderlength; | ||
147 | |||
148 | ape_ctx->firstframe = ape_ctx->junklength + ape_ctx->headerlength + | ||
149 | ape_ctx->wavheaderlength + ape_ctx->seektablelength; | ||
150 | } | ||
151 | |||
152 | ape_ctx->totalsamples = ape_ctx->finalframeblocks; | ||
153 | if (ape_ctx->totalframes > 1) | ||
154 | ape_ctx->totalsamples += ape_ctx->blocksperframe * (ape_ctx->totalframes-1); | ||
155 | |||
156 | ape_ctx->numseekpoints = APE_MAX(ape_ctx->maxseekpoints, | ||
157 | ape_ctx->seektablelength / sizeof(int32_t)); | ||
158 | |||
159 | return 0; | ||
160 | } | ||
161 | |||
162 | |||
163 | #ifndef ROCKBOX | ||
164 | /* Helper functions */ | ||
165 | |||
166 | static int read_uint16(int fd, uint16_t* x) | ||
167 | { | ||
168 | unsigned char tmp[2]; | ||
169 | int n; | ||
170 | |||
171 | n = read(fd,tmp,2); | ||
172 | |||
173 | if (n != 2) | ||
174 | return -1; | ||
175 | |||
176 | *x = tmp[0] | (tmp[1] << 8); | ||
177 | |||
178 | return 0; | ||
179 | } | ||
180 | |||
181 | static int read_int16(int fd, int16_t* x) | ||
182 | { | ||
183 | return read_uint16(fd, (uint16_t*)x); | ||
184 | } | ||
185 | |||
186 | static int read_uint32(int fd, uint32_t* x) | ||
187 | { | ||
188 | unsigned char tmp[4]; | ||
189 | int n; | ||
190 | |||
191 | n = read(fd,tmp,4); | ||
192 | |||
193 | if (n != 4) | ||
194 | return -1; | ||
195 | |||
196 | *x = tmp[0] | (tmp[1] << 8) | (tmp[2] << 16) | (tmp[3] << 24); | ||
197 | |||
198 | return 0; | ||
199 | } | ||
200 | |||
201 | int ape_parseheader(int fd, struct ape_ctx_t* ape_ctx) | ||
202 | { | ||
203 | int i,n; | ||
204 | |||
205 | /* TODO: Skip any leading junk such as id3v2 tags */ | ||
206 | ape_ctx->junklength = 0; | ||
207 | |||
208 | lseek(fd,ape_ctx->junklength,SEEK_SET); | ||
209 | |||
210 | n = read(fd,&ape_ctx->magic,4); | ||
211 | if (n != 4) return -1; | ||
212 | |||
213 | if (memcmp(ape_ctx->magic,"MAC ",4)!=0) | ||
214 | { | ||
215 | return -1; | ||
216 | } | ||
217 | |||
218 | if (read_int16(fd,&ape_ctx->fileversion) < 0) | ||
219 | return -1; | ||
220 | |||
221 | if (ape_ctx->fileversion >= 3980) | ||
222 | { | ||
223 | if (read_int16(fd,&ape_ctx->padding1) < 0) | ||
224 | return -1; | ||
225 | if (read_uint32(fd,&ape_ctx->descriptorlength) < 0) | ||
226 | return -1; | ||
227 | if (read_uint32(fd,&ape_ctx->headerlength) < 0) | ||
228 | return -1; | ||
229 | if (read_uint32(fd,&ape_ctx->seektablelength) < 0) | ||
230 | return -1; | ||
231 | if (read_uint32(fd,&ape_ctx->wavheaderlength) < 0) | ||
232 | return -1; | ||
233 | if (read_uint32(fd,&ape_ctx->audiodatalength) < 0) | ||
234 | return -1; | ||
235 | if (read_uint32(fd,&ape_ctx->audiodatalength_high) < 0) | ||
236 | return -1; | ||
237 | if (read_uint32(fd,&ape_ctx->wavtaillength) < 0) | ||
238 | return -1; | ||
239 | if (read(fd,&ape_ctx->md5,16) != 16) | ||
240 | return -1; | ||
241 | |||
242 | /* Skip any unknown bytes at the end of the descriptor. This is for future | ||
243 | compatibility */ | ||
244 | if (ape_ctx->descriptorlength > 52) | ||
245 | lseek(fd,ape_ctx->descriptorlength - 52, SEEK_CUR); | ||
246 | |||
247 | /* Read header data */ | ||
248 | if (read_uint16(fd,&ape_ctx->compressiontype) < 0) | ||
249 | return -1; | ||
250 | if (read_uint16(fd,&ape_ctx->formatflags) < 0) | ||
251 | return -1; | ||
252 | if (read_uint32(fd,&ape_ctx->blocksperframe) < 0) | ||
253 | return -1; | ||
254 | if (read_uint32(fd,&ape_ctx->finalframeblocks) < 0) | ||
255 | return -1; | ||
256 | if (read_uint32(fd,&ape_ctx->totalframes) < 0) | ||
257 | return -1; | ||
258 | if (read_uint16(fd,&ape_ctx->bps) < 0) | ||
259 | return -1; | ||
260 | if (read_uint16(fd,&ape_ctx->channels) < 0) | ||
261 | return -1; | ||
262 | if (read_uint32(fd,&ape_ctx->samplerate) < 0) | ||
263 | return -1; | ||
264 | } else { | ||
265 | ape_ctx->descriptorlength = 0; | ||
266 | ape_ctx->headerlength = 32; | ||
267 | |||
268 | if (read_uint16(fd,&ape_ctx->compressiontype) < 0) | ||
269 | return -1; | ||
270 | if (read_uint16(fd,&ape_ctx->formatflags) < 0) | ||
271 | return -1; | ||
272 | if (read_uint16(fd,&ape_ctx->channels) < 0) | ||
273 | return -1; | ||
274 | if (read_uint32(fd,&ape_ctx->samplerate) < 0) | ||
275 | return -1; | ||
276 | if (read_uint32(fd,&ape_ctx->wavheaderlength) < 0) | ||
277 | return -1; | ||
278 | if (read_uint32(fd,&ape_ctx->wavtaillength) < 0) | ||
279 | return -1; | ||
280 | if (read_uint32(fd,&ape_ctx->totalframes) < 0) | ||
281 | return -1; | ||
282 | if (read_uint32(fd,&ape_ctx->finalframeblocks) < 0) | ||
283 | return -1; | ||
284 | |||
285 | if (ape_ctx->formatflags & MAC_FORMAT_FLAG_HAS_PEAK_LEVEL) | ||
286 | { | ||
287 | lseek(fd, 4, SEEK_CUR); /* Skip the peak level */ | ||
288 | ape_ctx->headerlength += 4; | ||
289 | } | ||
290 | |||
291 | if (ape_ctx->formatflags & MAC_FORMAT_FLAG_HAS_SEEK_ELEMENTS) | ||
292 | { | ||
293 | if (read_uint32(fd,&ape_ctx->seektablelength) < 0) | ||
294 | return -1; | ||
295 | ape_ctx->headerlength += 4; | ||
296 | ape_ctx->seektablelength *= sizeof(int32_t); | ||
297 | } else { | ||
298 | ape_ctx->seektablelength = ape_ctx->totalframes * sizeof(int32_t); | ||
299 | } | ||
300 | |||
301 | if (ape_ctx->formatflags & MAC_FORMAT_FLAG_8_BIT) | ||
302 | ape_ctx->bps = 8; | ||
303 | else if (ape_ctx->formatflags & MAC_FORMAT_FLAG_24_BIT) | ||
304 | ape_ctx->bps = 24; | ||
305 | else | ||
306 | ape_ctx->bps = 16; | ||
307 | |||
308 | if (ape_ctx->fileversion >= 3950) | ||
309 | ape_ctx->blocksperframe = 73728 * 4; | ||
310 | else if ((ape_ctx->fileversion >= 3900) || (ape_ctx->fileversion >= 3800 && ape_ctx->compressiontype >= 4000)) | ||
311 | ape_ctx->blocksperframe = 73728; | ||
312 | else | ||
313 | ape_ctx->blocksperframe = 9216; | ||
314 | |||
315 | /* Skip any stored wav header */ | ||
316 | if (!(ape_ctx->formatflags & MAC_FORMAT_FLAG_CREATE_WAV_HEADER)) | ||
317 | { | ||
318 | lseek(fd, ape_ctx->wavheaderlength, SEEK_CUR); | ||
319 | } | ||
320 | } | ||
321 | |||
322 | ape_ctx->totalsamples = ape_ctx->finalframeblocks; | ||
323 | if (ape_ctx->totalframes > 1) | ||
324 | ape_ctx->totalsamples += ape_ctx->blocksperframe * (ape_ctx->totalframes-1); | ||
325 | |||
326 | if (ape_ctx->seektablelength > 0) | ||
327 | { | ||
328 | ape_ctx->seektable = malloc(ape_ctx->seektablelength); | ||
329 | if (ape_ctx->seektable == NULL) | ||
330 | return -1; | ||
331 | for (i=0; i < ape_ctx->seektablelength / sizeof(uint32_t); i++) | ||
332 | { | ||
333 | if (read_uint32(fd,&ape_ctx->seektable[i]) < 0) | ||
334 | { | ||
335 | free(ape_ctx->seektable); | ||
336 | return -1; | ||
337 | } | ||
338 | } | ||
339 | } | ||
340 | |||
341 | ape_ctx->firstframe = ape_ctx->junklength + ape_ctx->descriptorlength + | ||
342 | ape_ctx->headerlength + ape_ctx->seektablelength + | ||
343 | ape_ctx->wavheaderlength; | ||
344 | |||
345 | return 0; | ||
346 | } | ||
347 | |||
348 | void ape_dumpinfo(struct ape_ctx_t* ape_ctx) | ||
349 | { | ||
350 | int i; | ||
351 | |||
352 | printf("Descriptor Block:\n\n"); | ||
353 | printf("magic = \"%c%c%c%c\"\n", | ||
354 | ape_ctx->magic[0],ape_ctx->magic[1], | ||
355 | ape_ctx->magic[2],ape_ctx->magic[3]); | ||
356 | printf("fileversion = %d\n",ape_ctx->fileversion); | ||
357 | printf("descriptorlength = %d\n",ape_ctx->descriptorlength); | ||
358 | printf("headerlength = %d\n",ape_ctx->headerlength); | ||
359 | printf("seektablelength = %d\n",ape_ctx->seektablelength); | ||
360 | printf("wavheaderlength = %d\n",ape_ctx->wavheaderlength); | ||
361 | printf("audiodatalength = %d\n",ape_ctx->audiodatalength); | ||
362 | printf("audiodatalength_high = %d\n",ape_ctx->audiodatalength_high); | ||
363 | printf("wavtaillength = %d\n",ape_ctx->wavtaillength); | ||
364 | printf("md5 = "); | ||
365 | for (i = 0; i < 16; i++) | ||
366 | printf("%02x",ape_ctx->md5[i]); | ||
367 | printf("\n"); | ||
368 | |||
369 | printf("\nHeader Block:\n\n"); | ||
370 | |||
371 | printf("compressiontype = %d\n",ape_ctx->compressiontype); | ||
372 | printf("formatflags = %d\n",ape_ctx->formatflags); | ||
373 | printf("blocksperframe = %d\n",ape_ctx->blocksperframe); | ||
374 | printf("finalframeblocks = %d\n",ape_ctx->finalframeblocks); | ||
375 | printf("totalframes = %d\n",ape_ctx->totalframes); | ||
376 | printf("bps = %d\n",ape_ctx->bps); | ||
377 | printf("channels = %d\n",ape_ctx->channels); | ||
378 | printf("samplerate = %d\n",ape_ctx->samplerate); | ||
379 | |||
380 | printf("\nSeektable\n\n"); | ||
381 | if ((ape_ctx->seektablelength / sizeof(uint32_t)) != ape_ctx->totalframes) | ||
382 | { | ||
383 | printf("No seektable\n"); | ||
384 | } | ||
385 | else | ||
386 | { | ||
387 | for ( i = 0; i < ape_ctx->seektablelength / sizeof(uint32_t) ; i++) | ||
388 | { | ||
389 | if (i < ape_ctx->totalframes-1) { | ||
390 | printf("%8d %d (%d bytes)\n",i,ape_ctx->seektable[i],ape_ctx->seektable[i+1]-ape_ctx->seektable[i]); | ||
391 | } else { | ||
392 | printf("%8d %d\n",i,ape_ctx->seektable[i]); | ||
393 | } | ||
394 | } | ||
395 | } | ||
396 | printf("\nCalculated information:\n\n"); | ||
397 | printf("junklength = %d\n",ape_ctx->junklength); | ||
398 | printf("firstframe = %d\n",ape_ctx->firstframe); | ||
399 | printf("totalsamples = %d\n",ape_ctx->totalsamples); | ||
400 | } | ||
401 | |||
402 | #endif /* !ROCKBOX */ | ||