Rename a file that had a confusing filename.
[deb_shairplay.git] / src / lib / alac / alac.c
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 #ifdef __BIG_ENDIAN__
33 static const int host_bigendian = 1;
34 #else
35 static const int host_bigendian = 0;
36 #endif
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #ifdef _WIN32
42 #include "stdint_win.h"
43 #else
44 #include <stdint.h>
45 #endif
46
47 #include "alac.h"
48
49 #define _Swap32(v) do { \
50 v = (((v) & 0x000000FF) << 0x18) | \
51 (((v) & 0x0000FF00) << 0x08) | \
52 (((v) & 0x00FF0000) >> 0x08) | \
53 (((v) & 0xFF000000) >> 0x18); } while(0)
54
55 #define _Swap16(v) do { \
56 v = (((v) & 0x00FF) << 0x08) | \
57 (((v) & 0xFF00) >> 0x08); } while (0)
58
59 struct {signed int x:24;} se_struct_24;
60 #define SignExtend24(val) (se_struct_24.x = val)
61
62 struct alac_file
63 {
64 unsigned char *input_buffer;
65 int input_buffer_bitaccumulator; /* used so we can do arbitary
66 bit reads */
67
68 int samplesize;
69 int numchannels;
70 int bytespersample;
71
72
73 /* buffers */
74 int32_t *predicterror_buffer_a;
75 int32_t *predicterror_buffer_b;
76
77 int32_t *outputsamples_buffer_a;
78 int32_t *outputsamples_buffer_b;
79
80 int32_t *uncompressed_bytes_buffer_a;
81 int32_t *uncompressed_bytes_buffer_b;
82
83
84
85 /* stuff from setinfo */
86 uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */ /* max samples per frame? */
87 uint8_t setinfo_7a; /* 0x00 */
88 uint8_t setinfo_sample_size; /* 0x10 */
89 uint8_t setinfo_rice_historymult; /* 0x28 */
90 uint8_t setinfo_rice_initialhistory; /* 0x0a */
91 uint8_t setinfo_rice_kmodifier; /* 0x0e */
92 uint8_t setinfo_7f; /* 0x02 */
93 uint16_t setinfo_80; /* 0x00ff */
94 uint32_t setinfo_82; /* 0x000020e7 */ /* max sample size?? */
95 uint32_t setinfo_86; /* 0x00069fe4 */ /* bit rate (avarge)?? */
96 uint32_t setinfo_8a_rate; /* 0x0000ac44 */
97 /* end setinfo stuff */
98
99 };
100
101
102 static void allocate_buffers(alac_file *alac)
103 {
104 alac->predicterror_buffer_a = malloc(alac->setinfo_max_samples_per_frame * 4);
105 alac->predicterror_buffer_b = malloc(alac->setinfo_max_samples_per_frame * 4);
106
107 alac->outputsamples_buffer_a = malloc(alac->setinfo_max_samples_per_frame * 4);
108 alac->outputsamples_buffer_b = malloc(alac->setinfo_max_samples_per_frame * 4);
109
110 alac->uncompressed_bytes_buffer_a = malloc(alac->setinfo_max_samples_per_frame * 4);
111 alac->uncompressed_bytes_buffer_b = malloc(alac->setinfo_max_samples_per_frame * 4);
112 }
113
114 void alac_set_info(alac_file *alac, char *inputbuffer)
115 {
116 char *ptr = inputbuffer;
117 ptr += 4; /* size */
118 ptr += 4; /* frma */
119 ptr += 4; /* alac */
120 ptr += 4; /* size */
121 ptr += 4; /* alac */
122
123 ptr += 4; /* 0 ? */
124
125 alac->setinfo_max_samples_per_frame = *(uint32_t*)ptr; /* buffer size / 2 ? */
126 if (!host_bigendian)
127 _Swap32(alac->setinfo_max_samples_per_frame);
128 ptr += 4;
129 alac->setinfo_7a = *(uint8_t*)ptr;
130 ptr += 1;
131 alac->setinfo_sample_size = *(uint8_t*)ptr;
132 ptr += 1;
133 alac->setinfo_rice_historymult = *(uint8_t*)ptr;
134 ptr += 1;
135 alac->setinfo_rice_initialhistory = *(uint8_t*)ptr;
136 ptr += 1;
137 alac->setinfo_rice_kmodifier = *(uint8_t*)ptr;
138 ptr += 1;
139 alac->setinfo_7f = *(uint8_t*)ptr;
140 ptr += 1;
141 alac->setinfo_80 = *(uint16_t*)ptr;
142 if (!host_bigendian)
143 _Swap16(alac->setinfo_80);
144 ptr += 2;
145 alac->setinfo_82 = *(uint32_t*)ptr;
146 if (!host_bigendian)
147 _Swap32(alac->setinfo_82);
148 ptr += 4;
149 alac->setinfo_86 = *(uint32_t*)ptr;
150 if (!host_bigendian)
151 _Swap32(alac->setinfo_86);
152 ptr += 4;
153 alac->setinfo_8a_rate = *(uint32_t*)ptr;
154 if (!host_bigendian)
155 _Swap32(alac->setinfo_8a_rate);
156 ptr += 4;
157
158 allocate_buffers(alac);
159
160 }
161
162 /* stream reading */
163
164 /* supports reading 1 to 16 bits, in big endian format */
165 static uint32_t readbits_16(alac_file *alac, int bits)
166 {
167 uint32_t result;
168 int new_accumulator;
169
170 result = (alac->input_buffer[0] << 16) |
171 (alac->input_buffer[1] << 8) |
172 (alac->input_buffer[2]);
173
174 /* shift left by the number of bits we've already read,
175 * so that the top 'n' bits of the 24 bits we read will
176 * be the return bits */
177 result = result << alac->input_buffer_bitaccumulator;
178
179 result = result & 0x00ffffff;
180
181 /* and then only want the top 'n' bits from that, where
182 * n is 'bits' */
183 result = result >> (24 - bits);
184
185 new_accumulator = (alac->input_buffer_bitaccumulator + bits);
186
187 /* increase the buffer pointer if we've read over n bytes. */
188 alac->input_buffer += (new_accumulator >> 3);
189
190 /* and the remainder goes back into the bit accumulator */
191 alac->input_buffer_bitaccumulator = (new_accumulator & 7);
192
193 return result;
194 }
195
196 /* supports reading 1 to 32 bits, in big endian format */
197 static uint32_t readbits(alac_file *alac, int bits)
198 {
199 int32_t result = 0;
200
201 if (bits > 16)
202 {
203 bits -= 16;
204 result = readbits_16(alac, 16) << bits;
205 }
206
207 result |= readbits_16(alac, bits);
208
209 return result;
210 }
211
212 /* reads a single bit */
213 static int readbit(alac_file *alac)
214 {
215 int result;
216 int new_accumulator;
217
218 result = alac->input_buffer[0];
219
220 result = result << alac->input_buffer_bitaccumulator;
221
222 result = result >> 7 & 1;
223
224 new_accumulator = (alac->input_buffer_bitaccumulator + 1);
225
226 alac->input_buffer += (new_accumulator / 8);
227
228 alac->input_buffer_bitaccumulator = (new_accumulator % 8);
229
230 return result;
231 }
232
233 static void unreadbits(alac_file *alac, int bits)
234 {
235 int new_accumulator = (alac->input_buffer_bitaccumulator - bits);
236
237 alac->input_buffer += (new_accumulator >> 3);
238
239 alac->input_buffer_bitaccumulator = (new_accumulator & 7);
240 if (alac->input_buffer_bitaccumulator < 0)
241 alac->input_buffer_bitaccumulator *= -1;
242 }
243
244 /* various implementations of count_leading_zero:
245 * the first one is the original one, the simplest and most
246 * obvious for what it's doing. never use this.
247 * then there are the asm ones. fill in as necessary
248 * and finally an unrolled and optimised c version
249 * to fall back to
250 */
251 #if 0
252 /* hideously inefficient. could use a bitmask search,
253 * alternatively bsr on x86,
254 */
255 static int count_leading_zeros(int32_t input)
256 {
257 int i = 0;
258 while (!(0x80000000 & input) && i < 32)
259 {
260 i++;
261 input = input << 1;
262 }
263 return i;
264 }
265 #elif defined(__GNUC__) && (defined(_X86) || defined(__i386) || defined(i386))
266 /* for some reason the unrolled version (below) is
267 * actually faster than this. yay intel!
268 */
269 static int count_leading_zeros(int input)
270 {
271 int output = 0;
272 if (!input) return 32;
273 __asm("bsr %1, %0\n"
274 : "=r" (output)
275 : "r" (input));
276 return (0x1f - output);
277 }
278 #elif defined(__GNUC__)
279 static int count_leading_zeros(int input)
280 {
281 return __builtin_clz(input);
282 }
283 #elif defined(_MSC_VER) && defined(_M_IX86)
284 static int count_leading_zeros(int input)
285 {
286 int output = 0;
287 if (!input) return 32;
288 __asm
289 {
290 mov eax, input;
291 mov edx, 0x1f;
292 bsr ecx, eax;
293 sub edx, ecx;
294 mov output, edx;
295 }
296 return output;
297 }
298 #else
299 #warning using generic count leading zeroes. You may wish to write one for your CPU / compiler
300 static int count_leading_zeros(int input)
301 {
302 int output = 0;
303 int curbyte = 0;
304
305 curbyte = input >> 24;
306 if (curbyte) goto found;
307 output += 8;
308
309 curbyte = input >> 16;
310 if (curbyte & 0xff) goto found;
311 output += 8;
312
313 curbyte = input >> 8;
314 if (curbyte & 0xff) goto found;
315 output += 8;
316
317 curbyte = input;
318 if (curbyte & 0xff) goto found;
319 output += 8;
320
321 return output;
322
323 found:
324 if (!(curbyte & 0xf0))
325 {
326 output += 4;
327 }
328 else
329 curbyte >>= 4;
330
331 if (curbyte & 0x8)
332 return output;
333 if (curbyte & 0x4)
334 return output + 1;
335 if (curbyte & 0x2)
336 return output + 2;
337 if (curbyte & 0x1)
338 return output + 3;
339
340 /* shouldn't get here: */
341 return output + 4;
342 }
343 #endif
344
345 #define RICE_THRESHOLD 8 // maximum number of bits for a rice prefix.
346
347 int32_t entropy_decode_value(alac_file* alac,
348 int readSampleSize,
349 int k,
350 int rice_kmodifier_mask)
351 {
352 int32_t x = 0; // decoded value
353
354 // read x, number of 1s before 0 represent the rice value.
355 while (x <= RICE_THRESHOLD && readbit(alac))
356 {
357 x++;
358 }
359
360 if (x > RICE_THRESHOLD)
361 {
362 // read the number from the bit stream (raw value)
363 int32_t value;
364
365 value = readbits(alac, readSampleSize);
366
367 // mask value
368 value &= (((uint32_t)0xffffffff) >> (32 - readSampleSize));
369
370 x = value;
371 }
372 else
373 {
374 if (k != 1)
375 {
376 int extraBits = readbits(alac, k);
377
378 // x = x * (2^k - 1)
379 x *= (((1 << k) - 1) & rice_kmodifier_mask);
380
381 if (extraBits > 1)
382 x += extraBits - 1;
383 else
384 unreadbits(alac, 1);
385 }
386 }
387
388 return x;
389 }
390
391 void entropy_rice_decode(alac_file* alac,
392 int32_t* outputBuffer,
393 int outputSize,
394 int readSampleSize,
395 int rice_initialhistory,
396 int rice_kmodifier,
397 int rice_historymult,
398 int rice_kmodifier_mask)
399 {
400 int outputCount;
401 int history = rice_initialhistory;
402 int signModifier = 0;
403
404 for (outputCount = 0; outputCount < outputSize; outputCount++)
405 {
406 int32_t decodedValue;
407 int32_t finalValue;
408 int32_t k;
409
410 k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3);
411
412 if (k < 0) k += rice_kmodifier;
413 else k = rice_kmodifier;
414
415 // note: don't use rice_kmodifier_mask here (set mask to 0xFFFFFFFF)
416 decodedValue = entropy_decode_value(alac, readSampleSize, k, 0xFFFFFFFF);
417
418 decodedValue += signModifier;
419 finalValue = (decodedValue + 1) / 2; // inc by 1 and shift out sign bit
420 if (decodedValue & 1) // the sign is stored in the low bit
421 finalValue *= -1;
422
423 outputBuffer[outputCount] = finalValue;
424
425 signModifier = 0;
426
427 // update history
428 history += (decodedValue * rice_historymult)
429 - ((history * rice_historymult) >> 9);
430
431 if (decodedValue > 0xFFFF)
432 history = 0xFFFF;
433
434 // special case, for compressed blocks of 0
435 if ((history < 128) && (outputCount + 1 < outputSize))
436 {
437 int32_t blockSize;
438
439 signModifier = 1;
440
441 k = count_leading_zeros(history) + ((history + 16) / 64) - 24;
442
443 // note: blockSize is always 16bit
444 blockSize = entropy_decode_value(alac, 16, k, rice_kmodifier_mask);
445
446 // got blockSize 0s
447 if (blockSize > 0)
448 {
449 memset(&outputBuffer[outputCount + 1], 0, blockSize * sizeof(*outputBuffer));
450 outputCount += blockSize;
451 }
452
453 if (blockSize > 0xFFFF)
454 signModifier = 0;
455
456 history = 0;
457 }
458 }
459 }
460
461 #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
462
463 #define SIGN_ONLY(v) \
464 ((v < 0) ? (-1) : \
465 ((v > 0) ? (1) : \
466 (0)))
467
468 static void predictor_decompress_fir_adapt(int32_t *error_buffer,
469 int32_t *buffer_out,
470 int output_size,
471 int readsamplesize,
472 int16_t *predictor_coef_table,
473 int predictor_coef_num,
474 int predictor_quantitization)
475 {
476 int i;
477
478 /* first sample always copies */
479 *buffer_out = *error_buffer;
480
481 if (!predictor_coef_num)
482 {
483 if (output_size <= 1) return;
484 memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
485 return;
486 }
487
488 if (predictor_coef_num == 0x1f) /* 11111 - max value of predictor_coef_num */
489 { /* second-best case scenario for fir decompression,
490 * error describes a small difference from the previous sample only
491 */
492 if (output_size <= 1) return;
493 for (i = 0; i < output_size - 1; i++)
494 {
495 int32_t prev_value;
496 int32_t error_value;
497
498 prev_value = buffer_out[i];
499 error_value = error_buffer[i+1];
500 buffer_out[i+1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize);
501 }
502 return;
503 }
504
505 /* read warm-up samples */
506 if (predictor_coef_num > 0)
507 {
508 int i;
509 for (i = 0; i < predictor_coef_num; i++)
510 {
511 int32_t val;
512
513 val = buffer_out[i] + error_buffer[i+1];
514
515 val = SIGN_EXTENDED32(val, readsamplesize);
516
517 buffer_out[i+1] = val;
518 }
519 }
520
521 #if 0
522 /* 4 and 8 are very common cases (the only ones i've seen). these
523 * should be unrolled and optimised
524 */
525 if (predictor_coef_num == 4)
526 {
527 /* FIXME: optimised general case */
528 return;
529 }
530
531 if (predictor_coef_table == 8)
532 {
533 /* FIXME: optimised general case */
534 return;
535 }
536 #endif
537
538
539 /* general case */
540 if (predictor_coef_num > 0)
541 {
542 for (i = predictor_coef_num + 1;
543 i < output_size;
544 i++)
545 {
546 int j;
547 int sum = 0;
548 int outval;
549 int error_val = error_buffer[i];
550
551 for (j = 0; j < predictor_coef_num; j++)
552 {
553 sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
554 predictor_coef_table[j];
555 }
556
557 outval = (1 << (predictor_quantitization-1)) + sum;
558 outval = outval >> predictor_quantitization;
559 outval = outval + buffer_out[0] + error_val;
560 outval = SIGN_EXTENDED32(outval, readsamplesize);
561
562 buffer_out[predictor_coef_num+1] = outval;
563
564 if (error_val > 0)
565 {
566 int predictor_num = predictor_coef_num - 1;
567
568 while (predictor_num >= 0 && error_val > 0)
569 {
570 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
571 int sign = SIGN_ONLY(val);
572
573 predictor_coef_table[predictor_num] -= sign;
574
575 val *= sign; /* absolute value */
576
577 error_val -= ((val >> predictor_quantitization) *
578 (predictor_coef_num - predictor_num));
579
580 predictor_num--;
581 }
582 }
583 else if (error_val < 0)
584 {
585 int predictor_num = predictor_coef_num - 1;
586
587 while (predictor_num >= 0 && error_val < 0)
588 {
589 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
590 int sign = - SIGN_ONLY(val);
591
592 predictor_coef_table[predictor_num] -= sign;
593
594 val *= sign; /* neg value */
595
596 error_val -= ((val >> predictor_quantitization) *
597 (predictor_coef_num - predictor_num));
598
599 predictor_num--;
600 }
601 }
602
603 buffer_out++;
604 }
605 }
606 }
607
608 void deinterlace_16(int32_t *buffer_a, int32_t *buffer_b,
609 int16_t *buffer_out,
610 int numchannels, int numsamples,
611 uint8_t interlacing_shift,
612 uint8_t interlacing_leftweight)
613 {
614 int i;
615 if (numsamples <= 0) return;
616
617 /* weighted interlacing */
618 if (interlacing_leftweight)
619 {
620 for (i = 0; i < numsamples; i++)
621 {
622 int32_t difference, midright;
623 int16_t left;
624 int16_t right;
625
626 midright = buffer_a[i];
627 difference = buffer_b[i];
628
629
630 right = midright - ((difference * interlacing_leftweight) >> interlacing_shift);
631 left = right + difference;
632
633 /* output is always little endian */
634 if (host_bigendian)
635 {
636 _Swap16(left);
637 _Swap16(right);
638 }
639
640 buffer_out[i*numchannels] = left;
641 buffer_out[i*numchannels + 1] = right;
642 }
643
644 return;
645 }
646
647 /* otherwise basic interlacing took place */
648 for (i = 0; i < numsamples; i++)
649 {
650 int16_t left, right;
651
652 left = buffer_a[i];
653 right = buffer_b[i];
654
655 /* output is always little endian */
656 if (host_bigendian)
657 {
658 _Swap16(left);
659 _Swap16(right);
660 }
661
662 buffer_out[i*numchannels] = left;
663 buffer_out[i*numchannels + 1] = right;
664 }
665 }
666
667 void deinterlace_24(int32_t *buffer_a, int32_t *buffer_b,
668 int uncompressed_bytes,
669 int32_t *uncompressed_bytes_buffer_a, int32_t *uncompressed_bytes_buffer_b,
670 void *buffer_out,
671 int numchannels, int numsamples,
672 uint8_t interlacing_shift,
673 uint8_t interlacing_leftweight)
674 {
675 int i;
676 if (numsamples <= 0) return;
677
678 /* weighted interlacing */
679 if (interlacing_leftweight)
680 {
681 for (i = 0; i < numsamples; i++)
682 {
683 int32_t difference, midright;
684 int32_t left;
685 int32_t right;
686
687 midright = buffer_a[i];
688 difference = buffer_b[i];
689
690 right = midright - ((difference * interlacing_leftweight) >> interlacing_shift);
691 left = right + difference;
692
693 if (uncompressed_bytes)
694 {
695 uint32_t mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8));
696 left <<= (uncompressed_bytes * 8);
697 right <<= (uncompressed_bytes * 8);
698
699 left |= uncompressed_bytes_buffer_a[i] & mask;
700 right |= uncompressed_bytes_buffer_b[i] & mask;
701 }
702
703 ((uint8_t*)buffer_out)[i * numchannels * 3] = (left) & 0xFF;
704 ((uint8_t*)buffer_out)[i * numchannels * 3 + 1] = (left >> 8) & 0xFF;
705 ((uint8_t*)buffer_out)[i * numchannels * 3 + 2] = (left >> 16) & 0xFF;
706
707 ((uint8_t*)buffer_out)[i * numchannels * 3 + 3] = (right) & 0xFF;
708 ((uint8_t*)buffer_out)[i * numchannels * 3 + 4] = (right >> 8) & 0xFF;
709 ((uint8_t*)buffer_out)[i * numchannels * 3 + 5] = (right >> 16) & 0xFF;
710 }
711
712 return;
713 }
714
715 /* otherwise basic interlacing took place */
716 for (i = 0; i < numsamples; i++)
717 {
718 int32_t left, right;
719
720 left = buffer_a[i];
721 right = buffer_b[i];
722
723 if (uncompressed_bytes)
724 {
725 uint32_t mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8));
726 left <<= (uncompressed_bytes * 8);
727 right <<= (uncompressed_bytes * 8);
728
729 left |= uncompressed_bytes_buffer_a[i] & mask;
730 right |= uncompressed_bytes_buffer_b[i] & mask;
731 }
732
733 ((uint8_t*)buffer_out)[i * numchannels * 3] = (left) & 0xFF;
734 ((uint8_t*)buffer_out)[i * numchannels * 3 + 1] = (left >> 8) & 0xFF;
735 ((uint8_t*)buffer_out)[i * numchannels * 3 + 2] = (left >> 16) & 0xFF;
736
737 ((uint8_t*)buffer_out)[i * numchannels * 3 + 3] = (right) & 0xFF;
738 ((uint8_t*)buffer_out)[i * numchannels * 3 + 4] = (right >> 8) & 0xFF;
739 ((uint8_t*)buffer_out)[i * numchannels * 3 + 5] = (right >> 16) & 0xFF;
740
741 }
742
743 }
744
745 void decode_frame(alac_file *alac,
746 unsigned char *inbuffer,
747 void *outbuffer, int *outputsize)
748 {
749 int channels;
750 int32_t outputsamples = alac->setinfo_max_samples_per_frame;
751
752 /* setup the stream */
753 alac->input_buffer = inbuffer;
754 alac->input_buffer_bitaccumulator = 0;
755
756 channels = readbits(alac, 3);
757
758 *outputsize = outputsamples * alac->bytespersample;
759
760 switch(channels)
761 {
762 case 0: /* 1 channel */
763 {
764 int hassize;
765 int isnotcompressed;
766 int readsamplesize;
767
768 int uncompressed_bytes;
769 int ricemodifier;
770
771 /* 2^result = something to do with output waiting.
772 * perhaps matters if we read > 1 frame in a pass?
773 */
774 readbits(alac, 4);
775
776 readbits(alac, 12); /* unknown, skip 12 bits */
777
778 hassize = readbits(alac, 1); /* the output sample size is stored soon */
779
780 uncompressed_bytes = readbits(alac, 2); /* number of bytes in the (compressed) stream that are not compressed */
781
782 isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
783
784 if (hassize)
785 {
786 /* now read the number of samples,
787 * as a 32bit integer */
788 outputsamples = readbits(alac, 32);
789 *outputsize = outputsamples * alac->bytespersample;
790 }
791
792 readsamplesize = alac->setinfo_sample_size - (uncompressed_bytes * 8);
793
794 if (!isnotcompressed)
795 { /* so it is compressed */
796 int16_t predictor_coef_table[32];
797 int predictor_coef_num;
798 int prediction_type;
799 int prediction_quantitization;
800 int i;
801
802 /* skip 16 bits, not sure what they are. seem to be used in
803 * two channel case */
804 readbits(alac, 8);
805 readbits(alac, 8);
806
807 prediction_type = readbits(alac, 4);
808 prediction_quantitization = readbits(alac, 4);
809
810 ricemodifier = readbits(alac, 3);
811 predictor_coef_num = readbits(alac, 5);
812
813 /* read the predictor table */
814 for (i = 0; i < predictor_coef_num; i++)
815 {
816 predictor_coef_table[i] = (int16_t)readbits(alac, 16);
817 }
818
819 if (uncompressed_bytes)
820 {
821 int i;
822 for (i = 0; i < outputsamples; i++)
823 {
824 alac->uncompressed_bytes_buffer_a[i] = readbits(alac, uncompressed_bytes * 8);
825 }
826 }
827
828 entropy_rice_decode(alac,
829 alac->predicterror_buffer_a,
830 outputsamples,
831 readsamplesize,
832 alac->setinfo_rice_initialhistory,
833 alac->setinfo_rice_kmodifier,
834 ricemodifier * alac->setinfo_rice_historymult / 4,
835 (1 << alac->setinfo_rice_kmodifier) - 1);
836
837 if (prediction_type == 0)
838 { /* adaptive fir */
839 predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
840 alac->outputsamples_buffer_a,
841 outputsamples,
842 readsamplesize,
843 predictor_coef_table,
844 predictor_coef_num,
845 prediction_quantitization);
846 }
847 else
848 {
849 fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type);
850 /* i think the only other prediction type (or perhaps this is just a
851 * boolean?) runs adaptive fir twice.. like:
852 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
853 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
854 * little strange..
855 */
856 }
857
858 }
859 else
860 { /* not compressed, easy case */
861 if (alac->setinfo_sample_size <= 16)
862 {
863 int i;
864 for (i = 0; i < outputsamples; i++)
865 {
866 int32_t audiobits = readbits(alac, alac->setinfo_sample_size);
867
868 audiobits = SIGN_EXTENDED32(audiobits, alac->setinfo_sample_size);
869
870 alac->outputsamples_buffer_a[i] = audiobits;
871 }
872 }
873 else
874 {
875 int i;
876 for (i = 0; i < outputsamples; i++)
877 {
878 int32_t audiobits;
879
880 audiobits = readbits(alac, 16);
881 /* special case of sign extension..
882 * as we'll be ORing the low 16bits into this */
883 audiobits = audiobits << (alac->setinfo_sample_size - 16);
884 audiobits |= readbits(alac, alac->setinfo_sample_size - 16);
885 audiobits = SignExtend24(audiobits);
886
887 alac->outputsamples_buffer_a[i] = audiobits;
888 }
889 }
890 uncompressed_bytes = 0; // always 0 for uncompressed
891 }
892
893 switch(alac->setinfo_sample_size)
894 {
895 case 16:
896 {
897 int i;
898 for (i = 0; i < outputsamples; i++)
899 {
900 int16_t sample = alac->outputsamples_buffer_a[i];
901 if (host_bigendian)
902 _Swap16(sample);
903 ((int16_t*)outbuffer)[i * alac->numchannels] = sample;
904 }
905 break;
906 }
907 case 24:
908 {
909 int i;
910 for (i = 0; i < outputsamples; i++)
911 {
912 int32_t sample = alac->outputsamples_buffer_a[i];
913
914 if (uncompressed_bytes)
915 {
916 uint32_t mask;
917 sample = sample << (uncompressed_bytes * 8);
918 mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8));
919 sample |= alac->uncompressed_bytes_buffer_a[i] & mask;
920 }
921
922 ((uint8_t*)outbuffer)[i * alac->numchannels * 3] = (sample) & 0xFF;
923 ((uint8_t*)outbuffer)[i * alac->numchannels * 3 + 1] = (sample >> 8) & 0xFF;
924 ((uint8_t*)outbuffer)[i * alac->numchannels * 3 + 2] = (sample >> 16) & 0xFF;
925 }
926 break;
927 }
928 case 20:
929 case 32:
930 fprintf(stderr, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
931 break;
932 default:
933 break;
934 }
935 break;
936 }
937 case 1: /* 2 channels */
938 {
939 int hassize;
940 int isnotcompressed;
941 int readsamplesize;
942
943 int uncompressed_bytes;
944
945 uint8_t interlacing_shift;
946 uint8_t interlacing_leftweight;
947
948 /* 2^result = something to do with output waiting.
949 * perhaps matters if we read > 1 frame in a pass?
950 */
951 readbits(alac, 4);
952
953 readbits(alac, 12); /* unknown, skip 12 bits */
954
955 hassize = readbits(alac, 1); /* the output sample size is stored soon */
956
957 uncompressed_bytes = readbits(alac, 2); /* the number of bytes in the (compressed) stream that are not compressed */
958
959 isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
960
961 if (hassize)
962 {
963 /* now read the number of samples,
964 * as a 32bit integer */
965 outputsamples = readbits(alac, 32);
966 *outputsize = outputsamples * alac->bytespersample;
967 }
968
969 readsamplesize = alac->setinfo_sample_size - (uncompressed_bytes * 8) + 1;
970
971 if (!isnotcompressed)
972 { /* compressed */
973 int16_t predictor_coef_table_a[32];
974 int predictor_coef_num_a;
975 int prediction_type_a;
976 int prediction_quantitization_a;
977 int ricemodifier_a;
978
979 int16_t predictor_coef_table_b[32];
980 int predictor_coef_num_b;
981 int prediction_type_b;
982 int prediction_quantitization_b;
983 int ricemodifier_b;
984
985 int i;
986
987 interlacing_shift = readbits(alac, 8);
988 interlacing_leftweight = readbits(alac, 8);
989
990 /******** channel 1 ***********/
991 prediction_type_a = readbits(alac, 4);
992 prediction_quantitization_a = readbits(alac, 4);
993
994 ricemodifier_a = readbits(alac, 3);
995 predictor_coef_num_a = readbits(alac, 5);
996
997 /* read the predictor table */
998 for (i = 0; i < predictor_coef_num_a; i++)
999 {
1000 predictor_coef_table_a[i] = (int16_t)readbits(alac, 16);
1001 }
1002
1003 /******** channel 2 *********/
1004 prediction_type_b = readbits(alac, 4);
1005 prediction_quantitization_b = readbits(alac, 4);
1006
1007 ricemodifier_b = readbits(alac, 3);
1008 predictor_coef_num_b = readbits(alac, 5);
1009
1010 /* read the predictor table */
1011 for (i = 0; i < predictor_coef_num_b; i++)
1012 {
1013 predictor_coef_table_b[i] = (int16_t)readbits(alac, 16);
1014 }
1015
1016 /*********************/
1017 if (uncompressed_bytes)
1018 { /* see mono case */
1019 int i;
1020 for (i = 0; i < outputsamples; i++)
1021 {
1022 alac->uncompressed_bytes_buffer_a[i] = readbits(alac, uncompressed_bytes * 8);
1023 alac->uncompressed_bytes_buffer_b[i] = readbits(alac, uncompressed_bytes * 8);
1024 }
1025 }
1026
1027 /* channel 1 */
1028 entropy_rice_decode(alac,
1029 alac->predicterror_buffer_a,
1030 outputsamples,
1031 readsamplesize,
1032 alac->setinfo_rice_initialhistory,
1033 alac->setinfo_rice_kmodifier,
1034 ricemodifier_a * alac->setinfo_rice_historymult / 4,
1035 (1 << alac->setinfo_rice_kmodifier) - 1);
1036
1037 if (prediction_type_a == 0)
1038 { /* adaptive fir */
1039 predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
1040 alac->outputsamples_buffer_a,
1041 outputsamples,
1042 readsamplesize,
1043 predictor_coef_table_a,
1044 predictor_coef_num_a,
1045 prediction_quantitization_a);
1046 }
1047 else
1048 { /* see mono case */
1049 fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_a);
1050 }
1051
1052 /* channel 2 */
1053 entropy_rice_decode(alac,
1054 alac->predicterror_buffer_b,
1055 outputsamples,
1056 readsamplesize,
1057 alac->setinfo_rice_initialhistory,
1058 alac->setinfo_rice_kmodifier,
1059 ricemodifier_b * alac->setinfo_rice_historymult / 4,
1060 (1 << alac->setinfo_rice_kmodifier) - 1);
1061
1062 if (prediction_type_b == 0)
1063 { /* adaptive fir */
1064 predictor_decompress_fir_adapt(alac->predicterror_buffer_b,
1065 alac->outputsamples_buffer_b,
1066 outputsamples,
1067 readsamplesize,
1068 predictor_coef_table_b,
1069 predictor_coef_num_b,
1070 prediction_quantitization_b);
1071 }
1072 else
1073 {
1074 fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_b);
1075 }
1076 }
1077 else
1078 { /* not compressed, easy case */
1079 if (alac->setinfo_sample_size <= 16)
1080 {
1081 int i;
1082 for (i = 0; i < outputsamples; i++)
1083 {
1084 int32_t audiobits_a, audiobits_b;
1085
1086 audiobits_a = readbits(alac, alac->setinfo_sample_size);
1087 audiobits_b = readbits(alac, alac->setinfo_sample_size);
1088
1089 audiobits_a = SIGN_EXTENDED32(audiobits_a, alac->setinfo_sample_size);
1090 audiobits_b = SIGN_EXTENDED32(audiobits_b, alac->setinfo_sample_size);
1091
1092 alac->outputsamples_buffer_a[i] = audiobits_a;
1093 alac->outputsamples_buffer_b[i] = audiobits_b;
1094 }
1095 }
1096 else
1097 {
1098 int i;
1099 for (i = 0; i < outputsamples; i++)
1100 {
1101 int32_t audiobits_a, audiobits_b;
1102
1103 audiobits_a = readbits(alac, 16);
1104 audiobits_a = audiobits_a << (alac->setinfo_sample_size - 16);
1105 audiobits_a |= readbits(alac, alac->setinfo_sample_size - 16);
1106 audiobits_a = SignExtend24(audiobits_a);
1107
1108 audiobits_b = readbits(alac, 16);
1109 audiobits_b = audiobits_b << (alac->setinfo_sample_size - 16);
1110 audiobits_b |= readbits(alac, alac->setinfo_sample_size - 16);
1111 audiobits_b = SignExtend24(audiobits_b);
1112
1113 alac->outputsamples_buffer_a[i] = audiobits_a;
1114 alac->outputsamples_buffer_b[i] = audiobits_b;
1115 }
1116 }
1117 uncompressed_bytes = 0; // always 0 for uncompressed
1118 interlacing_shift = 0;
1119 interlacing_leftweight = 0;
1120 }
1121
1122 switch(alac->setinfo_sample_size)
1123 {
1124 case 16:
1125 {
1126 deinterlace_16(alac->outputsamples_buffer_a,
1127 alac->outputsamples_buffer_b,
1128 (int16_t*)outbuffer,
1129 alac->numchannels,
1130 outputsamples,
1131 interlacing_shift,
1132 interlacing_leftweight);
1133 break;
1134 }
1135 case 24:
1136 {
1137 deinterlace_24(alac->outputsamples_buffer_a,
1138 alac->outputsamples_buffer_b,
1139 uncompressed_bytes,
1140 alac->uncompressed_bytes_buffer_a,
1141 alac->uncompressed_bytes_buffer_b,
1142 (int16_t*)outbuffer,
1143 alac->numchannels,
1144 outputsamples,
1145 interlacing_shift,
1146 interlacing_leftweight);
1147 break;
1148 }
1149 case 20:
1150 case 32:
1151 fprintf(stderr, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
1152 break;
1153 default:
1154 break;
1155 }
1156
1157 break;
1158 }
1159 }
1160 }
1161
1162 alac_file *create_alac(int samplesize, int numchannels)
1163 {
1164 alac_file *newfile = malloc(sizeof(alac_file));
1165
1166 newfile->samplesize = samplesize;
1167 newfile->numchannels = numchannels;
1168 newfile->bytespersample = (samplesize / 8) * numchannels;
1169
1170 return newfile;
1171 }
1172