diff options
Diffstat (limited to 'apps/codecs/libalac/alac.c')
-rw-r--r-- | apps/codecs/libalac/alac.c | 972 |
1 files changed, 972 insertions, 0 deletions
diff --git a/apps/codecs/libalac/alac.c b/apps/codecs/libalac/alac.c new file mode 100644 index 0000000000..575bbf5abc --- /dev/null +++ b/apps/codecs/libalac/alac.c | |||
@@ -0,0 +1,972 @@ | |||
1 | /* | ||
2 | * ALAC (Apple Lossless Audio Codec) decoder | ||
3 | * Copyright (c) 2005 David Hammerton | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * This is the actual decoder. | ||
7 | * | ||
8 | * http://crazney.net/programs/itunes/alac.html | ||
9 | * | ||
10 | * Permission is hereby granted, free of charge, to any person | ||
11 | * obtaining a copy of this software and associated documentation | ||
12 | * files (the "Software"), to deal in the Software without | ||
13 | * restriction, including without limitation the rights to use, | ||
14 | * copy, modify, merge, publish, distribute, sublicense, and/or | ||
15 | * sell copies of the Software, and to permit persons to whom the | ||
16 | * Software is furnished to do so, subject to the following conditions: | ||
17 | * | ||
18 | * The above copyright notice and this permission notice shall be | ||
19 | * included in all copies or substantial portions of the Software. | ||
20 | * | ||
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
28 | * OTHER DEALINGS IN THE SOFTWARE. | ||
29 | * | ||
30 | */ | ||
31 | |||
32 | |||
33 | #include <stdio.h> | ||
34 | #include <stdlib.h> | ||
35 | #include <string.h> | ||
36 | #include <stdint.h> | ||
37 | |||
38 | #include "decomp.h" | ||
39 | |||
40 | #define _Swap32(v) do { \ | ||
41 | v = (((v) & 0x000000FF) << 0x18) | \ | ||
42 | (((v) & 0x0000FF00) << 0x08) | \ | ||
43 | (((v) & 0x00FF0000) >> 0x08) | \ | ||
44 | (((v) & 0xFF000000) >> 0x18); } while(0) | ||
45 | |||
46 | #define _Swap16(v) do { \ | ||
47 | v = (((v) & 0x00FF) << 0x08) | \ | ||
48 | (((v) & 0xFF00) >> 0x08); } while (0) | ||
49 | |||
50 | |||
51 | extern int host_bigendian; | ||
52 | |||
53 | struct alac_file | ||
54 | { | ||
55 | unsigned char *input_buffer; | ||
56 | int input_buffer_bitaccumulator; /* used so we can do arbitary | ||
57 | bit reads */ | ||
58 | |||
59 | int samplesize; | ||
60 | int numchannels; | ||
61 | int bytespersample; | ||
62 | |||
63 | |||
64 | /* buffers */ | ||
65 | int32_t *predicterror_buffer_a; | ||
66 | int32_t *predicterror_buffer_b; | ||
67 | |||
68 | int32_t *outputsamples_buffer_a; | ||
69 | int32_t *outputsamples_buffer_b; | ||
70 | |||
71 | |||
72 | /* stuff from setinfo */ | ||
73 | uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */ /* max samples per frame? */ | ||
74 | uint8_t setinfo_7a; /* 0x00 */ | ||
75 | uint8_t setinfo_sample_size; /* 0x10 */ | ||
76 | uint8_t setinfo_rice_historymult; /* 0x28 */ | ||
77 | uint8_t setinfo_rice_initialhistory; /* 0x0a */ | ||
78 | uint8_t setinfo_rice_kmodifier; /* 0x0e */ | ||
79 | uint8_t setinfo_7f; /* 0x02 */ | ||
80 | uint16_t setinfo_80; /* 0x00ff */ | ||
81 | uint32_t setinfo_82; /* 0x000020e7 */ | ||
82 | uint32_t setinfo_86; /* 0x00069fe4 */ | ||
83 | uint32_t setinfo_8a_rate; /* 0x0000ac44 */ | ||
84 | /* end setinfo stuff */ | ||
85 | |||
86 | }; | ||
87 | |||
88 | |||
89 | static void allocate_buffers(alac_file *alac) | ||
90 | { | ||
91 | alac->predicterror_buffer_a = malloc(alac->setinfo_max_samples_per_frame * 4); | ||
92 | alac->predicterror_buffer_b = malloc(alac->setinfo_max_samples_per_frame * 4); | ||
93 | |||
94 | alac->outputsamples_buffer_a = malloc(alac->setinfo_max_samples_per_frame * 4); | ||
95 | alac->outputsamples_buffer_b = malloc(alac->setinfo_max_samples_per_frame * 4); | ||
96 | } | ||
97 | |||
98 | void alac_set_info(alac_file *alac, char *inputbuffer) | ||
99 | { | ||
100 | char *ptr = inputbuffer; | ||
101 | ptr += 4; /* size */ | ||
102 | ptr += 4; /* frma */ | ||
103 | ptr += 4; /* alac */ | ||
104 | ptr += 4; /* size */ | ||
105 | ptr += 4; /* alac */ | ||
106 | |||
107 | ptr += 4; /* 0 ? */ | ||
108 | |||
109 | alac->setinfo_max_samples_per_frame = *(uint32_t*)ptr; /* buffer size / 2 ? */ | ||
110 | if (!host_bigendian) | ||
111 | _Swap32(alac->setinfo_max_samples_per_frame); | ||
112 | ptr += 4; | ||
113 | alac->setinfo_7a = *(uint8_t*)ptr; | ||
114 | ptr += 1; | ||
115 | alac->setinfo_sample_size = *(uint8_t*)ptr; | ||
116 | ptr += 1; | ||
117 | alac->setinfo_rice_historymult = *(uint8_t*)ptr; | ||
118 | ptr += 1; | ||
119 | alac->setinfo_rice_initialhistory = *(uint8_t*)ptr; | ||
120 | ptr += 1; | ||
121 | alac->setinfo_rice_kmodifier = *(uint8_t*)ptr; | ||
122 | ptr += 1; | ||
123 | alac->setinfo_7f = *(uint8_t*)ptr; | ||
124 | ptr += 1; | ||
125 | alac->setinfo_80 = *(uint16_t*)ptr; | ||
126 | if (!host_bigendian) | ||
127 | _Swap16(alac->setinfo_80); | ||
128 | ptr += 2; | ||
129 | alac->setinfo_82 = *(uint32_t*)ptr; | ||
130 | if (!host_bigendian) | ||
131 | _Swap32(alac->setinfo_82); | ||
132 | ptr += 4; | ||
133 | alac->setinfo_86 = *(uint32_t*)ptr; | ||
134 | if (!host_bigendian) | ||
135 | _Swap32(alac->setinfo_86); | ||
136 | ptr += 4; | ||
137 | alac->setinfo_8a_rate = *(uint32_t*)ptr; | ||
138 | if (!host_bigendian) | ||
139 | _Swap32(alac->setinfo_8a_rate); | ||
140 | ptr += 4; | ||
141 | |||
142 | allocate_buffers(alac); | ||
143 | |||
144 | } | ||
145 | |||
146 | /* stream reading */ | ||
147 | |||
148 | /* supports reading 1 to 16 bits, in big endian format */ | ||
149 | static uint32_t readbits_16(alac_file *alac, int bits) | ||
150 | { | ||
151 | uint32_t result; | ||
152 | int new_accumulator; | ||
153 | |||
154 | result = (alac->input_buffer[0] << 16) | | ||
155 | (alac->input_buffer[1] << 8) | | ||
156 | (alac->input_buffer[2]); | ||
157 | |||
158 | /* shift left by the number of bits we've already read, | ||
159 | * so that the top 'n' bits of the 24 bits we read will | ||
160 | * be the return bits */ | ||
161 | result = result << alac->input_buffer_bitaccumulator; | ||
162 | |||
163 | result = result & 0x00ffffff; | ||
164 | |||
165 | /* and then only want the top 'n' bits from that, where | ||
166 | * n is 'bits' */ | ||
167 | result = result >> (24 - bits); | ||
168 | |||
169 | new_accumulator = (alac->input_buffer_bitaccumulator + bits); | ||
170 | |||
171 | /* increase the buffer pointer if we've read over n bytes. */ | ||
172 | alac->input_buffer += (new_accumulator >> 3); | ||
173 | |||
174 | /* and the remainder goes back into the bit accumulator */ | ||
175 | alac->input_buffer_bitaccumulator = (new_accumulator & 7); | ||
176 | |||
177 | return result; | ||
178 | } | ||
179 | |||
180 | /* supports reading 1 to 32 bits, in big endian format */ | ||
181 | static uint32_t readbits(alac_file *alac, int bits) | ||
182 | { | ||
183 | int32_t result = 0; | ||
184 | |||
185 | if (bits > 16) | ||
186 | { | ||
187 | bits -= 16; | ||
188 | result = readbits_16(alac, 16) << bits; | ||
189 | } | ||
190 | |||
191 | result |= readbits_16(alac, bits); | ||
192 | |||
193 | return result; | ||
194 | } | ||
195 | |||
196 | /* reads a single bit */ | ||
197 | static int readbit(alac_file *alac) | ||
198 | { | ||
199 | int result; | ||
200 | int new_accumulator; | ||
201 | |||
202 | result = alac->input_buffer[0]; | ||
203 | |||
204 | result = result << alac->input_buffer_bitaccumulator; | ||
205 | |||
206 | result = result >> 7 & 1; | ||
207 | |||
208 | new_accumulator = (alac->input_buffer_bitaccumulator + 1); | ||
209 | |||
210 | alac->input_buffer += (new_accumulator / 8); | ||
211 | |||
212 | alac->input_buffer_bitaccumulator = (new_accumulator % 8); | ||
213 | |||
214 | return result; | ||
215 | } | ||
216 | |||
217 | static void unreadbits(alac_file *alac, int bits) | ||
218 | { | ||
219 | int new_accumulator = (alac->input_buffer_bitaccumulator - bits); | ||
220 | |||
221 | alac->input_buffer += (new_accumulator >> 3); | ||
222 | |||
223 | alac->input_buffer_bitaccumulator = (new_accumulator & 7); | ||
224 | if (alac->input_buffer_bitaccumulator < 0) | ||
225 | alac->input_buffer_bitaccumulator *= -1; | ||
226 | } | ||
227 | |||
228 | /* hideously inefficient. could use a bitmask search, | ||
229 | * alternatively bsr on x86, | ||
230 | */ | ||
231 | static int count_leading_zeros(int32_t input) | ||
232 | { | ||
233 | int i = 0; | ||
234 | while (!(0x80000000 & input) && i < 32) | ||
235 | { | ||
236 | i++; | ||
237 | input = input << 1; | ||
238 | } | ||
239 | return i; | ||
240 | } | ||
241 | |||
242 | void basterdised_rice_decompress(alac_file *alac, | ||
243 | int32_t *output_buffer, | ||
244 | int output_size, | ||
245 | int readsamplesize, /* arg_10 */ | ||
246 | int rice_initialhistory, /* arg424->b */ | ||
247 | int rice_kmodifier, /* arg424->d */ | ||
248 | int rice_historymult, /* arg424->c */ | ||
249 | int rice_kmodifier_mask /* arg424->e */ | ||
250 | ) | ||
251 | { | ||
252 | int output_count; | ||
253 | unsigned int history = rice_initialhistory; | ||
254 | int sign_modifier = 0; | ||
255 | |||
256 | for (output_count = 0; output_count < output_size; output_count++) | ||
257 | { | ||
258 | int32_t x = 0; | ||
259 | int32_t x_modified; | ||
260 | int32_t final_val; | ||
261 | |||
262 | /* read x - number of 1s before 0 represent the rice */ | ||
263 | while (x <= 8 && readbit(alac)) | ||
264 | { | ||
265 | x++; | ||
266 | } | ||
267 | |||
268 | |||
269 | if (x > 8) /* RICE THRESHOLD */ | ||
270 | { /* use alternative encoding */ | ||
271 | int32_t value; | ||
272 | |||
273 | value = readbits(alac, readsamplesize); | ||
274 | |||
275 | /* mask value to readsamplesize size */ | ||
276 | if (readsamplesize != 32) | ||
277 | value &= (0xffffffff >> (32 - readsamplesize)); | ||
278 | |||
279 | x = value; | ||
280 | } | ||
281 | else | ||
282 | { /* standard rice encoding */ | ||
283 | int extrabits; | ||
284 | int k; /* size of extra bits */ | ||
285 | |||
286 | /* read k, that is bits as is */ | ||
287 | k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3); | ||
288 | |||
289 | if (k < 0) k += rice_kmodifier; | ||
290 | else k = rice_kmodifier; | ||
291 | |||
292 | if (k != 1) | ||
293 | { | ||
294 | extrabits = readbits(alac, k); | ||
295 | |||
296 | /* multiply x by 2^k - 1, as part of their strange algorithm */ | ||
297 | x = (x << k) - x; | ||
298 | |||
299 | if (extrabits > 1) | ||
300 | { | ||
301 | x += extrabits - 1; | ||
302 | } | ||
303 | else unreadbits(alac, 1); | ||
304 | } | ||
305 | } | ||
306 | |||
307 | x_modified = sign_modifier + x; | ||
308 | final_val = (x_modified + 1) / 2; | ||
309 | if (x_modified & 1) final_val *= -1; | ||
310 | |||
311 | output_buffer[output_count] = final_val; | ||
312 | |||
313 | sign_modifier = 0; | ||
314 | |||
315 | /* now update the history */ | ||
316 | history += (x_modified * rice_historymult) | ||
317 | - ((history * rice_historymult) >> 9); | ||
318 | |||
319 | if (x_modified > 0xffff) | ||
320 | history = 0xffff; | ||
321 | |||
322 | /* special case: there may be compressed blocks of 0 */ | ||
323 | if ((history < 128) && (output_count+1 < output_size)) | ||
324 | { | ||
325 | int block_size; | ||
326 | |||
327 | sign_modifier = 1; | ||
328 | |||
329 | x = 0; | ||
330 | while (x <= 8 && readbit(alac)) | ||
331 | { | ||
332 | x++; | ||
333 | } | ||
334 | |||
335 | if (x > 8) | ||
336 | { | ||
337 | block_size = readbits(alac, 16); | ||
338 | block_size &= 0xffff; | ||
339 | } | ||
340 | else | ||
341 | { | ||
342 | int k; | ||
343 | int extrabits; | ||
344 | |||
345 | k = count_leading_zeros(history) + ((history + 16) >> 6 /* / 64 */) - 24; | ||
346 | |||
347 | extrabits = readbits(alac, k); | ||
348 | |||
349 | block_size = (((1 << k) - 1) & rice_kmodifier_mask) * x | ||
350 | + extrabits - 1; | ||
351 | |||
352 | if (extrabits < 2) | ||
353 | { | ||
354 | x = 1 - extrabits; | ||
355 | block_size += x; | ||
356 | unreadbits(alac, 1); | ||
357 | } | ||
358 | } | ||
359 | |||
360 | if (block_size > 0) | ||
361 | { | ||
362 | memset(&output_buffer[output_count+1], 0, block_size * 4); | ||
363 | output_count += block_size; | ||
364 | |||
365 | } | ||
366 | |||
367 | if (block_size > 0xffff) | ||
368 | sign_modifier = 0; | ||
369 | |||
370 | history = 0; | ||
371 | } | ||
372 | } | ||
373 | } | ||
374 | |||
375 | #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits)) | ||
376 | |||
377 | #define SIGN_ONLY(v) \ | ||
378 | ((v < 0) ? (-1) : \ | ||
379 | ((v > 0) ? (1) : \ | ||
380 | (0))) | ||
381 | |||
382 | static void predictor_decompress_fir_adapt(int32_t *error_buffer, | ||
383 | int32_t *buffer_out, | ||
384 | int output_size, | ||
385 | int readsamplesize, | ||
386 | int16_t *predictor_coef_table, | ||
387 | int predictor_coef_num, | ||
388 | int predictor_quantitization) | ||
389 | { | ||
390 | int i; | ||
391 | |||
392 | /* first sample always copies */ | ||
393 | *buffer_out = *error_buffer; | ||
394 | |||
395 | if (!predictor_coef_num) | ||
396 | { | ||
397 | if (output_size <= 1) return; | ||
398 | memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4); | ||
399 | return; | ||
400 | } | ||
401 | |||
402 | if (predictor_coef_num == 0x1f) /* 11111 - max value of predictor_coef_num */ | ||
403 | { /* second-best case scenario for fir decompression, | ||
404 | * error describes a small difference from the previous sample only | ||
405 | */ | ||
406 | if (output_size <= 1) return; | ||
407 | for (i = 0; i < output_size - 1; i++) | ||
408 | { | ||
409 | int32_t prev_value; | ||
410 | int32_t error_value; | ||
411 | |||
412 | prev_value = buffer_out[i]; | ||
413 | error_value = error_buffer[i+1]; | ||
414 | buffer_out[i+1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize); | ||
415 | } | ||
416 | return; | ||
417 | } | ||
418 | |||
419 | /* read warm-up samples */ | ||
420 | if (predictor_coef_num > 0) | ||
421 | { | ||
422 | int i; | ||
423 | for (i = 0; i < predictor_coef_num; i++) | ||
424 | { | ||
425 | int32_t val; | ||
426 | |||
427 | val = buffer_out[i] + error_buffer[i+1]; | ||
428 | |||
429 | val = SIGN_EXTENDED32(val, readsamplesize); | ||
430 | |||
431 | buffer_out[i+1] = val; | ||
432 | } | ||
433 | } | ||
434 | |||
435 | #if 0 | ||
436 | /* 4 and 8 are very common cases (the only ones i've seen). these | ||
437 | * should be unrolled and optimised | ||
438 | */ | ||
439 | if (predictor_coef_num == 4) | ||
440 | { | ||
441 | /* FIXME: optimised general case */ | ||
442 | return; | ||
443 | } | ||
444 | |||
445 | if (predictor_coef_table == 8) | ||
446 | { | ||
447 | /* FIXME: optimised general case */ | ||
448 | return; | ||
449 | } | ||
450 | #endif | ||
451 | |||
452 | |||
453 | /* general case */ | ||
454 | if (predictor_coef_num > 0) | ||
455 | { | ||
456 | for (i = predictor_coef_num + 1; | ||
457 | i < output_size; | ||
458 | i++) | ||
459 | { | ||
460 | int j; | ||
461 | int sum = 0; | ||
462 | int outval; | ||
463 | int error_val = error_buffer[i]; | ||
464 | |||
465 | for (j = 0; j < predictor_coef_num; j++) | ||
466 | { | ||
467 | sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) * | ||
468 | predictor_coef_table[j]; | ||
469 | } | ||
470 | |||
471 | outval = (1 << (predictor_quantitization-1)) + sum; | ||
472 | outval = outval >> predictor_quantitization; | ||
473 | outval = outval + buffer_out[0] + error_val; | ||
474 | outval = SIGN_EXTENDED32(outval, readsamplesize); | ||
475 | |||
476 | buffer_out[predictor_coef_num+1] = outval; | ||
477 | |||
478 | if (error_val > 0) | ||
479 | { | ||
480 | int predictor_num = predictor_coef_num - 1; | ||
481 | |||
482 | while (predictor_num >= 0 && error_val > 0) | ||
483 | { | ||
484 | int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; | ||
485 | int sign = SIGN_ONLY(val); | ||
486 | |||
487 | predictor_coef_table[predictor_num] -= sign; | ||
488 | |||
489 | val *= sign; /* absolute value */ | ||
490 | |||
491 | error_val -= ((val >> predictor_quantitization) * | ||
492 | (predictor_coef_num - predictor_num)); | ||
493 | |||
494 | predictor_num--; | ||
495 | } | ||
496 | } | ||
497 | else if (error_val < 0) | ||
498 | { | ||
499 | int predictor_num = predictor_coef_num - 1; | ||
500 | |||
501 | while (predictor_num >= 0 && error_val < 0) | ||
502 | { | ||
503 | int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; | ||
504 | int sign = - SIGN_ONLY(val); | ||
505 | |||
506 | predictor_coef_table[predictor_num] -= sign; | ||
507 | |||
508 | val *= sign; /* neg value */ | ||
509 | |||
510 | error_val -= ((val >> predictor_quantitization) * | ||
511 | (predictor_coef_num - predictor_num)); | ||
512 | |||
513 | predictor_num--; | ||
514 | } | ||
515 | } | ||
516 | |||
517 | buffer_out++; | ||
518 | } | ||
519 | } | ||
520 | } | ||
521 | |||
522 | void deinterlace_16(int32_t *buffer_a, int32_t *buffer_b, | ||
523 | int16_t *buffer_out, | ||
524 | int numchannels, int numsamples, | ||
525 | uint8_t interlacing_shift, | ||
526 | uint8_t interlacing_leftweight) | ||
527 | { | ||
528 | int i; | ||
529 | if (numsamples <= 0) return; | ||
530 | |||
531 | /* weighted interlacing */ | ||
532 | if (interlacing_leftweight) | ||
533 | { | ||
534 | for (i = 0; i < numsamples; i++) | ||
535 | { | ||
536 | int32_t difference, midright; | ||
537 | int16_t left; | ||
538 | int16_t right; | ||
539 | |||
540 | midright = buffer_a[i]; | ||
541 | difference = buffer_b[i]; | ||
542 | |||
543 | |||
544 | right = midright - ((difference * interlacing_leftweight) >> interlacing_shift); | ||
545 | left = (midright - ((difference * interlacing_leftweight) >> interlacing_shift)) | ||
546 | + difference; | ||
547 | |||
548 | /* output is always little endian */ | ||
549 | if (host_bigendian) | ||
550 | { | ||
551 | _Swap16(left); | ||
552 | _Swap16(right); | ||
553 | } | ||
554 | |||
555 | buffer_out[i*numchannels] = left; | ||
556 | buffer_out[i*numchannels + 1] = right; | ||
557 | } | ||
558 | |||
559 | return; | ||
560 | } | ||
561 | |||
562 | /* otherwise basic interlacing took place */ | ||
563 | for (i = 0; i < numsamples; i++) | ||
564 | { | ||
565 | int16_t left, right; | ||
566 | |||
567 | left = buffer_a[i]; | ||
568 | right = buffer_b[i]; | ||
569 | |||
570 | /* output is always little endian */ | ||
571 | if (host_bigendian) | ||
572 | { | ||
573 | _Swap16(left); | ||
574 | _Swap16(right); | ||
575 | } | ||
576 | |||
577 | buffer_out[i*numchannels] = left; | ||
578 | buffer_out[i*numchannels + 1] = right; | ||
579 | } | ||
580 | } | ||
581 | |||
582 | void decode_frame(alac_file *alac, | ||
583 | unsigned char *inbuffer, | ||
584 | void *outbuffer, int *outputsize) | ||
585 | { | ||
586 | int channels; | ||
587 | int32_t outputsamples = alac->setinfo_max_samples_per_frame; | ||
588 | |||
589 | /* setup the stream */ | ||
590 | alac->input_buffer = inbuffer; | ||
591 | alac->input_buffer_bitaccumulator = 0; | ||
592 | |||
593 | channels = readbits(alac, 3); | ||
594 | |||
595 | *outputsize = outputsamples * alac->bytespersample; | ||
596 | |||
597 | switch(channels) | ||
598 | { | ||
599 | case 0: /* 1 channel */ | ||
600 | { | ||
601 | int hassize; | ||
602 | int isnotcompressed; | ||
603 | int readsamplesize; | ||
604 | |||
605 | int wasted_bytes; | ||
606 | int ricemodifier; | ||
607 | |||
608 | |||
609 | /* 2^result = something to do with output waiting. | ||
610 | * perhaps matters if we read > 1 frame in a pass? | ||
611 | */ | ||
612 | readbits(alac, 4); | ||
613 | |||
614 | readbits(alac, 12); /* unknown, skip 12 bits */ | ||
615 | |||
616 | hassize = readbits(alac, 1); /* the output sample size is stored soon */ | ||
617 | |||
618 | wasted_bytes = readbits(alac, 2); /* unknown ? */ | ||
619 | |||
620 | isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */ | ||
621 | |||
622 | if (hassize) | ||
623 | { | ||
624 | /* now read the number of samples, | ||
625 | * as a 32bit integer */ | ||
626 | outputsamples = readbits(alac, 32); | ||
627 | *outputsize = outputsamples * alac->bytespersample; | ||
628 | } | ||
629 | |||
630 | readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8); | ||
631 | |||
632 | if (!isnotcompressed) | ||
633 | { /* so it is compressed */ | ||
634 | int16_t predictor_coef_table[32]; | ||
635 | int predictor_coef_num; | ||
636 | int prediction_type; | ||
637 | int prediction_quantitization; | ||
638 | int i; | ||
639 | |||
640 | /* skip 16 bits, not sure what they are. seem to be used in | ||
641 | * two channel case */ | ||
642 | readbits(alac, 8); | ||
643 | readbits(alac, 8); | ||
644 | |||
645 | prediction_type = readbits(alac, 4); | ||
646 | prediction_quantitization = readbits(alac, 4); | ||
647 | |||
648 | ricemodifier = readbits(alac, 3); | ||
649 | predictor_coef_num = readbits(alac, 5); | ||
650 | |||
651 | /* read the predictor table */ | ||
652 | for (i = 0; i < predictor_coef_num; i++) | ||
653 | { | ||
654 | predictor_coef_table[i] = (int16_t)readbits(alac, 16); | ||
655 | } | ||
656 | |||
657 | if (wasted_bytes) | ||
658 | { | ||
659 | /* these bytes seem to have something to do with | ||
660 | * > 2 channel files. | ||
661 | */ | ||
662 | fprintf(stderr, "FIXME: unimplemented, unhandling of wasted_bytes\n"); | ||
663 | } | ||
664 | |||
665 | basterdised_rice_decompress(alac, | ||
666 | alac->predicterror_buffer_a, | ||
667 | outputsamples, | ||
668 | readsamplesize, | ||
669 | alac->setinfo_rice_initialhistory, | ||
670 | alac->setinfo_rice_kmodifier, | ||
671 | ricemodifier * alac->setinfo_rice_historymult / 4, | ||
672 | (1 << alac->setinfo_rice_kmodifier) - 1); | ||
673 | |||
674 | if (prediction_type == 0) | ||
675 | { /* adaptive fir */ | ||
676 | predictor_decompress_fir_adapt(alac->predicterror_buffer_a, | ||
677 | alac->outputsamples_buffer_a, | ||
678 | outputsamples, | ||
679 | readsamplesize, | ||
680 | predictor_coef_table, | ||
681 | predictor_coef_num, | ||
682 | prediction_quantitization); | ||
683 | } | ||
684 | else | ||
685 | { | ||
686 | fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type); | ||
687 | /* i think the only other prediction type (or perhaps this is just a | ||
688 | * boolean?) runs adaptive fir twice.. like: | ||
689 | * predictor_decompress_fir_adapt(predictor_error, tempout, ...) | ||
690 | * predictor_decompress_fir_adapt(predictor_error, outputsamples ...) | ||
691 | * little strange.. | ||
692 | */ | ||
693 | } | ||
694 | |||
695 | } | ||
696 | else | ||
697 | { /* not compressed, easy case */ | ||
698 | if (readsamplesize <= 16) | ||
699 | { | ||
700 | int i; | ||
701 | for (i = 0; i < outputsamples; i++) | ||
702 | { | ||
703 | int32_t audiobits = readbits(alac, readsamplesize); | ||
704 | |||
705 | audiobits = SIGN_EXTENDED32(audiobits, readsamplesize); | ||
706 | |||
707 | alac->outputsamples_buffer_a[i] = audiobits; | ||
708 | } | ||
709 | } | ||
710 | else | ||
711 | { | ||
712 | int i; | ||
713 | for (i = 0; i < outputsamples; i++) | ||
714 | { | ||
715 | int32_t audiobits; | ||
716 | |||
717 | audiobits = readbits(alac, 16); | ||
718 | /* special case of sign extension.. | ||
719 | * as we'll be ORing the low 16bits into this */ | ||
720 | audiobits = audiobits << 16; | ||
721 | audiobits = audiobits >> (32 - readsamplesize); | ||
722 | |||
723 | audiobits |= readbits(alac, readsamplesize - 16); | ||
724 | |||
725 | alac->outputsamples_buffer_a[i] = audiobits; | ||
726 | } | ||
727 | } | ||
728 | /* wasted_bytes = 0; // unused */ | ||
729 | } | ||
730 | |||
731 | switch(alac->setinfo_sample_size) | ||
732 | { | ||
733 | case 16: | ||
734 | { | ||
735 | int i; | ||
736 | for (i = 0; i < outputsamples; i++) | ||
737 | { | ||
738 | int16_t sample = alac->outputsamples_buffer_a[i]; | ||
739 | if (host_bigendian) | ||
740 | _Swap16(sample); | ||
741 | ((int16_t*)outbuffer)[i * alac->numchannels] = sample; | ||
742 | } | ||
743 | break; | ||
744 | } | ||
745 | case 20: | ||
746 | case 24: | ||
747 | case 32: | ||
748 | fprintf(stderr, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size); | ||
749 | break; | ||
750 | default: | ||
751 | break; | ||
752 | } | ||
753 | break; | ||
754 | } | ||
755 | case 1: /* 2 channels */ | ||
756 | { | ||
757 | int hassize; | ||
758 | int isnotcompressed; | ||
759 | int readsamplesize; | ||
760 | |||
761 | int wasted_bytes; | ||
762 | |||
763 | uint8_t interlacing_shift; | ||
764 | uint8_t interlacing_leftweight; | ||
765 | |||
766 | /* 2^result = something to do with output waiting. | ||
767 | * perhaps matters if we read > 1 frame in a pass? | ||
768 | */ | ||
769 | readbits(alac, 4); | ||
770 | |||
771 | readbits(alac, 12); /* unknown, skip 12 bits */ | ||
772 | |||
773 | hassize = readbits(alac, 1); /* the output sample size is stored soon */ | ||
774 | |||
775 | wasted_bytes = readbits(alac, 2); /* unknown ? */ | ||
776 | |||
777 | isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */ | ||
778 | |||
779 | if (hassize) | ||
780 | { | ||
781 | /* now read the number of samples, | ||
782 | * as a 32bit integer */ | ||
783 | outputsamples = readbits(alac, 32); | ||
784 | *outputsize = outputsamples * alac->bytespersample; | ||
785 | } | ||
786 | |||
787 | readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8) + 1; | ||
788 | |||
789 | if (!isnotcompressed) | ||
790 | { /* compressed */ | ||
791 | int16_t predictor_coef_table_a[32]; | ||
792 | int predictor_coef_num_a; | ||
793 | int prediction_type_a; | ||
794 | int prediction_quantitization_a; | ||
795 | int ricemodifier_a; | ||
796 | |||
797 | int16_t predictor_coef_table_b[32]; | ||
798 | int predictor_coef_num_b; | ||
799 | int prediction_type_b; | ||
800 | int prediction_quantitization_b; | ||
801 | int ricemodifier_b; | ||
802 | |||
803 | int i; | ||
804 | |||
805 | interlacing_shift = readbits(alac, 8); | ||
806 | interlacing_leftweight = readbits(alac, 8); | ||
807 | |||
808 | /******** channel 1 ***********/ | ||
809 | prediction_type_a = readbits(alac, 4); | ||
810 | prediction_quantitization_a = readbits(alac, 4); | ||
811 | |||
812 | ricemodifier_a = readbits(alac, 3); | ||
813 | predictor_coef_num_a = readbits(alac, 5); | ||
814 | |||
815 | /* read the predictor table */ | ||
816 | for (i = 0; i < predictor_coef_num_a; i++) | ||
817 | { | ||
818 | predictor_coef_table_a[i] = (int16_t)readbits(alac, 16); | ||
819 | } | ||
820 | |||
821 | /******** channel 2 *********/ | ||
822 | prediction_type_b = readbits(alac, 4); | ||
823 | prediction_quantitization_b = readbits(alac, 4); | ||
824 | |||
825 | ricemodifier_b = readbits(alac, 3); | ||
826 | predictor_coef_num_b = readbits(alac, 5); | ||
827 | |||
828 | /* read the predictor table */ | ||
829 | for (i = 0; i < predictor_coef_num_b; i++) | ||
830 | { | ||
831 | predictor_coef_table_b[i] = (int16_t)readbits(alac, 16); | ||
832 | } | ||
833 | |||
834 | /*********************/ | ||
835 | if (wasted_bytes) | ||
836 | { /* see mono case */ | ||
837 | fprintf(stderr, "FIXME: unimplemented, unhandling of wasted_bytes\n"); | ||
838 | } | ||
839 | |||
840 | /* channel 1 */ | ||
841 | basterdised_rice_decompress(alac, | ||
842 | alac->predicterror_buffer_a, | ||
843 | outputsamples, | ||
844 | readsamplesize, | ||
845 | alac->setinfo_rice_initialhistory, | ||
846 | alac->setinfo_rice_kmodifier, | ||
847 | ricemodifier_a * alac->setinfo_rice_historymult / 4, | ||
848 | (1 << alac->setinfo_rice_kmodifier) - 1); | ||
849 | |||
850 | if (prediction_type_a == 0) | ||
851 | { /* adaptive fir */ | ||
852 | predictor_decompress_fir_adapt(alac->predicterror_buffer_a, | ||
853 | alac->outputsamples_buffer_a, | ||
854 | outputsamples, | ||
855 | readsamplesize, | ||
856 | predictor_coef_table_a, | ||
857 | predictor_coef_num_a, | ||
858 | prediction_quantitization_a); | ||
859 | } | ||
860 | else | ||
861 | { /* see mono case */ | ||
862 | fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_a); | ||
863 | } | ||
864 | |||
865 | /* channel 2 */ | ||
866 | basterdised_rice_decompress(alac, | ||
867 | alac->predicterror_buffer_b, | ||
868 | outputsamples, | ||
869 | readsamplesize, | ||
870 | alac->setinfo_rice_initialhistory, | ||
871 | alac->setinfo_rice_kmodifier, | ||
872 | ricemodifier_b * alac->setinfo_rice_historymult / 4, | ||
873 | (1 << alac->setinfo_rice_kmodifier) - 1); | ||
874 | |||
875 | if (prediction_type_b == 0) | ||
876 | { /* adaptive fir */ | ||
877 | predictor_decompress_fir_adapt(alac->predicterror_buffer_b, | ||
878 | alac->outputsamples_buffer_b, | ||
879 | outputsamples, | ||
880 | readsamplesize, | ||
881 | predictor_coef_table_b, | ||
882 | predictor_coef_num_b, | ||
883 | prediction_quantitization_b); | ||
884 | } | ||
885 | else | ||
886 | { | ||
887 | fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_b); | ||
888 | } | ||
889 | } | ||
890 | else | ||
891 | { /* not compressed, easy case */ | ||
892 | if (alac->setinfo_sample_size <= 16) | ||
893 | { | ||
894 | int i; | ||
895 | for (i = 0; i < outputsamples; i++) | ||
896 | { | ||
897 | int32_t audiobits_a, audiobits_b; | ||
898 | |||
899 | audiobits_a = readbits(alac, alac->setinfo_sample_size); | ||
900 | audiobits_b = readbits(alac, alac->setinfo_sample_size); | ||
901 | |||
902 | audiobits_a = SIGN_EXTENDED32(audiobits_a, alac->setinfo_sample_size); | ||
903 | audiobits_b = SIGN_EXTENDED32(audiobits_b, alac->setinfo_sample_size); | ||
904 | |||
905 | alac->outputsamples_buffer_a[i] = audiobits_a; | ||
906 | alac->outputsamples_buffer_b[i] = audiobits_b; | ||
907 | } | ||
908 | } | ||
909 | else | ||
910 | { | ||
911 | int i; | ||
912 | for (i = 0; i < outputsamples; i++) | ||
913 | { | ||
914 | int32_t audiobits_a, audiobits_b; | ||
915 | |||
916 | audiobits_a = readbits(alac, 16); | ||
917 | audiobits_a = audiobits_a << 16; | ||
918 | audiobits_a = audiobits_a >> (32 - alac->setinfo_sample_size); | ||
919 | audiobits_a |= readbits(alac, alac->setinfo_sample_size - 16); | ||
920 | |||
921 | audiobits_b = readbits(alac, 16); | ||
922 | audiobits_b = audiobits_b << 16; | ||
923 | audiobits_b = audiobits_b >> (32 - alac->setinfo_sample_size); | ||
924 | audiobits_b |= readbits(alac, alac->setinfo_sample_size - 16); | ||
925 | |||
926 | alac->outputsamples_buffer_a[i] = audiobits_a; | ||
927 | alac->outputsamples_buffer_b[i] = audiobits_b; | ||
928 | } | ||
929 | } | ||
930 | /* wasted_bytes = 0; */ | ||
931 | interlacing_shift = 0; | ||
932 | interlacing_leftweight = 0; | ||
933 | } | ||
934 | |||
935 | switch(alac->setinfo_sample_size) | ||
936 | { | ||
937 | case 16: | ||
938 | { | ||
939 | deinterlace_16(alac->outputsamples_buffer_a, | ||
940 | alac->outputsamples_buffer_b, | ||
941 | (int16_t*)outbuffer, | ||
942 | alac->numchannels, | ||
943 | outputsamples, | ||
944 | interlacing_shift, | ||
945 | interlacing_leftweight); | ||
946 | break; | ||
947 | } | ||
948 | case 20: | ||
949 | case 24: | ||
950 | case 32: | ||
951 | fprintf(stderr, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size); | ||
952 | break; | ||
953 | default: | ||
954 | break; | ||
955 | } | ||
956 | |||
957 | break; | ||
958 | } | ||
959 | } | ||
960 | } | ||
961 | |||
962 | alac_file *create_alac(int samplesize, int numchannels) | ||
963 | { | ||
964 | alac_file *newfile = malloc(sizeof(alac_file)); | ||
965 | |||
966 | newfile->samplesize = samplesize; | ||
967 | newfile->numchannels = numchannels; | ||
968 | newfile->bytespersample = (samplesize / 8) * numchannels; | ||
969 | |||
970 | return newfile; | ||
971 | } | ||
972 | |||