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