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