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