Correct beast manual install instructions in Windows.
[kugel-rb.git] / apps / codecs / libalac / alac.c
blob112ad20badd4efe40cf368b2feb1cb2f2159610b
1 /*
2 * ALAC (Apple Lossless Audio Codec) decoder
3 * Copyright (c) 2005 David Hammerton
4 * All rights reserved.
6 * This is the actual decoder.
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:
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
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.
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <inttypes.h>
38 #include "codeclib.h"
39 #include "decomp.h"
41 int16_t predictor_coef_table[32] IBSS_ATTR;
42 int16_t predictor_coef_table_a[32] IBSS_ATTR;
43 int16_t predictor_coef_table_b[32] IBSS_ATTR;
46 /* Endian/aligment safe functions - only used in alac_set_info() */
47 static uint32_t get_uint32be(unsigned char* p)
49 return((p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]);
52 static uint16_t get_uint16be(unsigned char* p)
54 return((p[0]<<8) | p[1]);
57 void alac_set_info(alac_file *alac, char *inputbuffer)
59 unsigned char* ptr = (unsigned char*)inputbuffer;
60 ptr += 4; /* size */
61 ptr += 4; /* frma */
62 ptr += 4; /* alac */
63 ptr += 4; /* size */
64 ptr += 4; /* alac */
66 ptr += 4; /* 0 ? */
68 alac->setinfo_max_samples_per_frame = get_uint32be(ptr); /* buffer size / 2 ? */
69 ptr += 4;
70 alac->setinfo_7a = *ptr++;
71 alac->setinfo_sample_size = *ptr++;
72 alac->setinfo_rice_historymult = *ptr++;
73 alac->setinfo_rice_initialhistory = *ptr++;
74 alac->setinfo_rice_kmodifier = *ptr++;
75 alac->setinfo_7f = *ptr++;
76 ptr += 1;
77 alac->setinfo_80 = get_uint16be(ptr);
78 ptr += 2;
79 alac->setinfo_82 = get_uint32be(ptr);
80 ptr += 4;
81 alac->setinfo_86 = get_uint32be(ptr);
82 ptr += 4;
83 alac->setinfo_8a_rate = get_uint32be(ptr);
84 ptr += 4;
87 /* stream reading */
89 /* supports reading 1 to 16 bits, in big endian format */
90 static inline uint32_t readbits_16(alac_file *alac, int bits)
92 uint32_t result;
93 int new_accumulator;
95 result = (alac->input_buffer[0] << 16) |
96 (alac->input_buffer[1] << 8) |
97 (alac->input_buffer[2]);
99 /* shift left by the number of bits we've already read,
100 * so that the top 'n' bits of the 24 bits we read will
101 * be the return bits */
102 result = result << alac->input_buffer_bitaccumulator;
104 result = result & 0x00ffffff;
106 /* and then only want the top 'n' bits from that, where
107 * n is 'bits' */
108 result = result >> (24 - bits);
110 new_accumulator = (alac->input_buffer_bitaccumulator + bits);
112 /* increase the buffer pointer if we've read over n bytes. */
113 alac->input_buffer += (new_accumulator >> 3);
115 /* and the remainder goes back into the bit accumulator */
116 alac->input_buffer_bitaccumulator = (new_accumulator & 7);
118 return result;
121 /* supports reading 1 to 32 bits, in big endian format */
122 static inline uint32_t readbits(alac_file *alac, int bits)
124 int32_t result = 0;
126 if (bits > 16)
128 bits -= 16;
129 result = readbits_16(alac, 16) << bits;
132 result |= readbits_16(alac, bits);
134 return result;
137 /* reads a single bit */
138 static inline int readbit(alac_file *alac)
140 int result;
141 int new_accumulator;
143 result = alac->input_buffer[0];
145 result = result << alac->input_buffer_bitaccumulator;
147 result = result >> 7 & 1;
149 new_accumulator = (alac->input_buffer_bitaccumulator + 1);
151 alac->input_buffer += (new_accumulator / 8);
153 alac->input_buffer_bitaccumulator = (new_accumulator % 8);
155 return result;
158 static inline void unreadbits(alac_file *alac, int bits)
160 int new_accumulator = (alac->input_buffer_bitaccumulator - bits);
162 alac->input_buffer += (new_accumulator >> 3);
164 alac->input_buffer_bitaccumulator = (new_accumulator & 7);
165 if (alac->input_buffer_bitaccumulator < 0)
166 alac->input_buffer_bitaccumulator *= -1;
169 static const unsigned char bittab[16] ICONST_ATTR = {
170 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4
173 static inline int count_leading_zeros(int input)
175 int output = 32;
177 #if 0
178 /* Experimentation has shown that the following test is always false,
179 so we don't bother to perform it. */
180 if (input & 0xffff0000)
182 input >>= 16;
183 output -= 16;
185 #endif
186 if (input & 0xff00)
188 input >>= 8;
189 output -= 8;
191 if (input & 0xf0)
193 input >>= 4;
194 output -= 4;
196 output -= bittab[input];
197 return output;
203 void basterdised_rice_decompress(alac_file *alac,
204 int32_t *output_buffer,
205 int output_size,
206 int readsamplesize, /* arg_10 */
207 int rice_initialhistory, /* arg424->b */
208 int rice_kmodifier, /* arg424->d */
209 int rice_historymult, /* arg424->c */
210 int rice_kmodifier_mask /* arg424->e */
211 ) ICODE_ATTR_ALAC;
212 void basterdised_rice_decompress(alac_file *alac,
213 int32_t *output_buffer,
214 int output_size,
215 int readsamplesize, /* arg_10 */
216 int rice_initialhistory, /* arg424->b */
217 int rice_kmodifier, /* arg424->d */
218 int rice_historymult, /* arg424->c */
219 int rice_kmodifier_mask /* arg424->e */
222 int output_count;
223 unsigned int history = rice_initialhistory;
224 int sign_modifier = 0;
226 for (output_count = 0; output_count < output_size; output_count++)
228 int32_t x = 0;
229 int32_t x_modified;
230 int32_t final_val;
232 /* read x - number of 1s before 0 represent the rice */
233 while (x <= 8 && readbit(alac))
235 x++;
239 if (x > 8) /* RICE THRESHOLD */
240 { /* use alternative encoding */
241 int32_t value;
243 value = readbits(alac, readsamplesize);
245 /* mask value to readsamplesize size */
246 if (readsamplesize != 32)
247 value &= (0xffffffff >> (32 - readsamplesize));
249 x = value;
251 else
252 { /* standard rice encoding */
253 int extrabits;
254 int k; /* size of extra bits */
256 /* read k, that is bits as is */
257 k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3);
259 if (k < 0) k += rice_kmodifier;
260 else k = rice_kmodifier;
262 if (k != 1)
264 extrabits = readbits(alac, k);
266 /* multiply x by 2^k - 1, as part of their strange algorithm */
267 x = (x << k) - x;
269 if (extrabits > 1)
271 x += extrabits - 1;
273 else unreadbits(alac, 1);
277 x_modified = sign_modifier + x;
278 final_val = (x_modified + 1) / 2;
279 if (x_modified & 1) final_val *= -1;
281 output_buffer[output_count] = final_val;
283 sign_modifier = 0;
285 /* now update the history */
286 history += (x_modified * rice_historymult)
287 - ((history * rice_historymult) >> 9);
289 if (x_modified > 0xffff)
290 history = 0xffff;
292 /* special case: there may be compressed blocks of 0 */
293 if ((history < 128) && (output_count+1 < output_size))
295 int block_size;
297 sign_modifier = 1;
299 x = 0;
300 while (x <= 8 && readbit(alac))
302 x++;
305 if (x > 8)
307 block_size = readbits(alac, 16);
308 block_size &= 0xffff;
310 else
312 int k;
313 int extrabits;
315 k = count_leading_zeros(history) + ((history + 16) >> 6 /* / 64 */) - 24;
317 extrabits = readbits(alac, k);
319 block_size = (((1 << k) - 1) & rice_kmodifier_mask) * x
320 + extrabits - 1;
322 if (extrabits < 2)
324 x = 1 - extrabits;
325 block_size += x;
326 unreadbits(alac, 1);
330 if (block_size > 0)
332 memset(&output_buffer[output_count+1], 0, block_size * 4);
333 output_count += block_size;
337 if (block_size > 0xffff)
338 sign_modifier = 0;
340 history = 0;
345 #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
347 #define SIGN_ONLY(v) \
348 ((v < 0) ? (-1) : \
349 ((v > 0) ? (1) : \
350 (0)))
352 static void predictor_decompress_fir_adapt(int32_t *error_buffer,
353 int32_t *buffer_out,
354 int output_size,
355 int readsamplesize,
356 int16_t *predictor_coef_table,
357 int predictor_coef_num,
358 int predictor_quantitization) ICODE_ATTR_ALAC;
359 static void predictor_decompress_fir_adapt(int32_t *error_buffer,
360 int32_t *buffer_out,
361 int output_size,
362 int readsamplesize,
363 int16_t *predictor_coef_table,
364 int predictor_coef_num,
365 int predictor_quantitization)
367 int i;
369 /* first sample always copies */
370 *buffer_out = *error_buffer;
372 if (!predictor_coef_num)
374 if (output_size <= 1) return;
375 memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
376 return;
379 if (predictor_coef_num == 0x1f) /* 11111 - max value of predictor_coef_num */
380 { /* second-best case scenario for fir decompression,
381 * error describes a small difference from the previous sample only
383 if (output_size <= 1) return;
384 for (i = 0; i < output_size - 1; i++)
386 int32_t prev_value;
387 int32_t error_value;
389 prev_value = buffer_out[i];
390 error_value = error_buffer[i+1];
391 buffer_out[i+1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize);
393 return;
396 /* read warm-up samples */
397 if (predictor_coef_num > 0)
399 int i;
400 for (i = 0; i < predictor_coef_num; i++)
402 int32_t val;
404 val = buffer_out[i] + error_buffer[i+1];
406 val = SIGN_EXTENDED32(val, readsamplesize);
408 buffer_out[i+1] = val;
412 /* 4 and 8 are very common cases (the only ones i've seen).
414 The following code is an initial attempt to unroll and optimise
415 these two cases by the Rockbox project. More work is needed.
418 /* optimised case: 4 */
419 if (predictor_coef_num == 4)
421 for (i = 4 + 1; i < output_size; i++)
423 int sum = 0;
424 int outval;
425 int error_val = error_buffer[i];
427 sum = (buffer_out[4] - buffer_out[0]) * predictor_coef_table[0]
428 + (buffer_out[3] - buffer_out[0]) * predictor_coef_table[1]
429 + (buffer_out[2] - buffer_out[0]) * predictor_coef_table[2]
430 + (buffer_out[1] - buffer_out[0]) * predictor_coef_table[3];
432 outval = (1 << (predictor_quantitization-1)) + sum;
433 outval = outval >> predictor_quantitization;
434 outval = outval + buffer_out[0] + error_val;
435 outval = SIGN_EXTENDED32(outval, readsamplesize);
437 buffer_out[4+1] = outval;
439 if (error_val > 0)
441 int predictor_num = 4 - 1;
443 while (predictor_num >= 0 && error_val > 0)
445 int val = buffer_out[0] - buffer_out[4 - predictor_num];
447 if (val!=0) {
448 if (val < 0) {
449 predictor_coef_table[predictor_num]++;
450 val=-val;
451 } else {
452 predictor_coef_table[predictor_num]--;
454 error_val -= ((val >> predictor_quantitization) * (4 - predictor_num));
456 predictor_num--;
459 else if (error_val < 0)
461 int predictor_num = 4 - 1;
463 while (predictor_num >= 0 && error_val < 0)
465 int val = buffer_out[0] - buffer_out[4 - predictor_num];
467 if (val != 0) {
468 if (val > 0) {
469 predictor_coef_table[predictor_num]++;
470 val=-val; /* neg value */
471 } else {
472 predictor_coef_table[predictor_num]--;
474 error_val -= ((val >> predictor_quantitization) * (4 - predictor_num));
476 predictor_num--;
480 buffer_out++;
482 return;
485 /* optimised case: 8 */
486 if (predictor_coef_num == 8)
488 for (i = 8 + 1;
489 i < output_size;
490 i++)
492 int sum;
493 int outval;
494 int error_val = error_buffer[i];
496 sum = (buffer_out[8] - buffer_out[0]) * predictor_coef_table[0]
497 + (buffer_out[7] - buffer_out[0]) * predictor_coef_table[1]
498 + (buffer_out[6] - buffer_out[0]) * predictor_coef_table[2]
499 + (buffer_out[5] - buffer_out[0]) * predictor_coef_table[3]
500 + (buffer_out[4] - buffer_out[0]) * predictor_coef_table[4]
501 + (buffer_out[3] - buffer_out[0]) * predictor_coef_table[5]
502 + (buffer_out[2] - buffer_out[0]) * predictor_coef_table[6]
503 + (buffer_out[1] - buffer_out[0]) * predictor_coef_table[7];
505 outval = (1 << (predictor_quantitization-1)) + sum;
506 outval = outval >> predictor_quantitization;
507 outval = outval + buffer_out[0] + error_val;
508 outval = SIGN_EXTENDED32(outval, readsamplesize);
510 buffer_out[8+1] = outval;
512 if (error_val > 0)
514 int predictor_num = 8 - 1;
516 while (predictor_num >= 0 && error_val > 0)
518 int val = buffer_out[0] - buffer_out[8 - predictor_num];
520 if (val!=0) {
521 if (val < 0) {
522 predictor_coef_table[predictor_num]++;
523 val=-val;
524 } else {
525 predictor_coef_table[predictor_num]--;
527 error_val -= ((val >> predictor_quantitization) * (8 - predictor_num));
529 predictor_num--;
532 else if (error_val < 0)
534 int predictor_num = 8 - 1;
536 while (predictor_num >= 0 && error_val < 0)
538 int val = buffer_out[0] - buffer_out[8 - predictor_num];
539 if (val != 0) {
540 if (val > 0) {
541 predictor_coef_table[predictor_num]++;
542 val=-val; /* neg value */
543 } else {
544 predictor_coef_table[predictor_num]--;
546 error_val -= ((val >> predictor_quantitization) * (8 - predictor_num));
548 predictor_num--;
552 buffer_out++;
554 return;
557 /* general case */
558 if (predictor_coef_num > 0)
560 for (i = predictor_coef_num + 1;
561 i < output_size;
562 i++)
564 int j;
565 int sum = 0;
566 int outval;
567 int error_val = error_buffer[i];
569 for (j = 0; j < predictor_coef_num; j++)
571 sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
572 predictor_coef_table[j];
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);
580 buffer_out[predictor_coef_num+1] = outval;
582 if (error_val > 0)
584 int predictor_num = predictor_coef_num - 1;
586 while (predictor_num >= 0 && error_val > 0)
588 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
589 int sign = SIGN_ONLY(val);
591 predictor_coef_table[predictor_num] -= sign;
593 val *= sign; /* absolute value */
595 error_val -= ((val >> predictor_quantitization) *
596 (predictor_coef_num - predictor_num));
598 predictor_num--;
601 else if (error_val < 0)
603 int predictor_num = predictor_coef_num - 1;
605 while (predictor_num >= 0 && error_val < 0)
607 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
608 int sign = - SIGN_ONLY(val);
610 predictor_coef_table[predictor_num] -= sign;
612 val *= sign; /* neg value */
614 error_val -= ((val >> predictor_quantitization) *
615 (predictor_coef_num - predictor_num));
617 predictor_num--;
621 buffer_out++;
626 void deinterlace_16(int32_t* buffer0,
627 int32_t* buffer1,
628 int numsamples,
629 uint8_t interlacing_shift,
630 uint8_t interlacing_leftweight) ICODE_ATTR_ALAC;
631 void deinterlace_16(int32_t* buffer0,
632 int32_t* buffer1,
633 int numsamples,
634 uint8_t interlacing_shift,
635 uint8_t interlacing_leftweight)
637 int i;
638 if (numsamples <= 0) return;
640 /* weighted interlacing */
641 if (interlacing_leftweight)
643 for (i = 0; i < numsamples; i++)
645 int32_t difference, midright;
647 midright = buffer0[i];
648 difference = buffer1[i];
650 buffer0[i] = ((midright - ((difference * interlacing_leftweight)
651 >> interlacing_shift)) + difference) << SCALE16;
652 buffer1[i] = (midright - ((difference * interlacing_leftweight)
653 >> interlacing_shift)) << SCALE16;
656 return;
659 /* otherwise basic interlacing took place */
660 for (i = 0; i < numsamples; i++)
662 buffer0[i] = buffer0[i] << SCALE16;
663 buffer1[i] = buffer1[i] << SCALE16;
668 static inline int decode_frame_mono(
669 alac_file *alac,
670 int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE],
671 void (*yield)(void))
673 int hassize;
674 int isnotcompressed;
675 int readsamplesize;
676 int outputsamples = alac->setinfo_max_samples_per_frame;
678 int wasted_bytes;
679 int ricemodifier;
682 /* 2^result = something to do with output waiting.
683 * perhaps matters if we read > 1 frame in a pass?
685 readbits(alac, 4);
687 readbits(alac, 12); /* unknown, skip 12 bits */
689 hassize = readbits(alac, 1); /* the output sample size is stored soon */
691 wasted_bytes = readbits(alac, 2); /* unknown ? */
693 isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
695 if (hassize)
697 /* now read the number of samples,
698 * as a 32bit integer */
699 outputsamples = readbits(alac, 32);
702 readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8);
704 if (!isnotcompressed)
705 { /* so it is compressed */
706 int predictor_coef_num;
707 int prediction_type;
708 int prediction_quantitization;
709 int i;
711 /* skip 16 bits, not sure what they are. seem to be used in
712 * two channel case */
713 readbits(alac, 8);
714 readbits(alac, 8);
716 prediction_type = readbits(alac, 4);
717 prediction_quantitization = readbits(alac, 4);
719 ricemodifier = readbits(alac, 3);
720 predictor_coef_num = readbits(alac, 5);
722 /* read the predictor table */
723 for (i = 0; i < predictor_coef_num; i++)
725 predictor_coef_table[i] = (int16_t)readbits(alac, 16);
728 if (wasted_bytes)
730 /* these bytes seem to have something to do with
731 * > 2 channel files.
733 //fprintf(stderr, "FIXME: unimplemented, unhandling of wasted_bytes\n");
736 yield();
738 basterdised_rice_decompress(alac,
739 outputbuffer[0],
740 outputsamples,
741 readsamplesize,
742 alac->setinfo_rice_initialhistory,
743 alac->setinfo_rice_kmodifier,
744 ricemodifier * alac->setinfo_rice_historymult / 4,
745 (1 << alac->setinfo_rice_kmodifier) - 1);
747 yield();
749 if (prediction_type == 0)
750 { /* adaptive fir */
751 predictor_decompress_fir_adapt(outputbuffer[0],
752 outputbuffer[0],
753 outputsamples,
754 readsamplesize,
755 predictor_coef_table,
756 predictor_coef_num,
757 prediction_quantitization);
759 else
761 //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type);
762 /* i think the only other prediction type (or perhaps this is just a
763 * boolean?) runs adaptive fir twice.. like:
764 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
765 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
766 * little strange..
771 else
772 { /* not compressed, easy case */
773 if (readsamplesize <= 16)
775 int i;
776 for (i = 0; i < outputsamples; i++)
778 int32_t audiobits = readbits(alac, readsamplesize);
780 audiobits = SIGN_EXTENDED32(audiobits, readsamplesize);
782 outputbuffer[0][i] = audiobits;
785 else
787 int i;
788 for (i = 0; i < outputsamples; i++)
790 int32_t audiobits;
792 audiobits = readbits(alac, 16);
793 /* special case of sign extension..
794 * as we'll be ORing the low 16bits into this */
795 audiobits = audiobits << 16;
796 audiobits = audiobits >> (32 - readsamplesize);
798 audiobits |= readbits(alac, readsamplesize - 16);
800 outputbuffer[0][i] = audiobits;
803 /* wasted_bytes = 0; // unused */
806 yield();
808 switch(alac->setinfo_sample_size)
810 case 16:
812 int i;
813 for (i = 0; i < outputsamples; i++)
815 /* Output mono data as stereo */
816 outputbuffer[0][i] = outputbuffer[0][i] << SCALE16;
817 outputbuffer[1][i] = outputbuffer[0][i];
819 break;
821 case 20:
822 case 24:
823 case 32:
824 //fprintf(stderr, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
825 break;
826 default:
827 break;
830 return outputsamples;
833 static inline int decode_frame_stereo(
834 alac_file *alac,
835 int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE],
836 void (*yield)(void))
838 int hassize;
839 int isnotcompressed;
840 int readsamplesize;
841 int outputsamples = alac->setinfo_max_samples_per_frame;
842 int wasted_bytes;
844 uint8_t interlacing_shift;
845 uint8_t interlacing_leftweight;
847 /* 2^result = something to do with output waiting.
848 * perhaps matters if we read > 1 frame in a pass?
850 readbits(alac, 4);
852 readbits(alac, 12); /* unknown, skip 12 bits */
854 hassize = readbits(alac, 1); /* the output sample size is stored soon */
856 wasted_bytes = readbits(alac, 2); /* unknown ? */
858 isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
860 if (hassize)
862 /* now read the number of samples,
863 * as a 32bit integer */
864 outputsamples = readbits(alac, 32);
867 readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8) + 1;
869 yield();
870 if (!isnotcompressed)
871 { /* compressed */
872 int predictor_coef_num_a;
873 int prediction_type_a;
874 int prediction_quantitization_a;
875 int ricemodifier_a;
877 int predictor_coef_num_b;
878 int prediction_type_b;
879 int prediction_quantitization_b;
880 int ricemodifier_b;
882 int i;
884 interlacing_shift = readbits(alac, 8);
885 interlacing_leftweight = readbits(alac, 8);
887 /******** channel 1 ***********/
888 prediction_type_a = readbits(alac, 4);
889 prediction_quantitization_a = readbits(alac, 4);
891 ricemodifier_a = readbits(alac, 3);
892 predictor_coef_num_a = readbits(alac, 5);
894 /* read the predictor table */
895 for (i = 0; i < predictor_coef_num_a; i++)
897 predictor_coef_table_a[i] = (int16_t)readbits(alac, 16);
900 /******** channel 2 *********/
901 prediction_type_b = readbits(alac, 4);
902 prediction_quantitization_b = readbits(alac, 4);
904 ricemodifier_b = readbits(alac, 3);
905 predictor_coef_num_b = readbits(alac, 5);
907 /* read the predictor table */
908 for (i = 0; i < predictor_coef_num_b; i++)
910 predictor_coef_table_b[i] = (int16_t)readbits(alac, 16);
913 /*********************/
914 if (wasted_bytes)
915 { /* see mono case */
916 //fprintf(stderr, "FIXME: unimplemented, unhandling of wasted_bytes\n");
919 yield();
920 /* channel 1 */
921 basterdised_rice_decompress(alac,
922 outputbuffer[0],
923 outputsamples,
924 readsamplesize,
925 alac->setinfo_rice_initialhistory,
926 alac->setinfo_rice_kmodifier,
927 ricemodifier_a * alac->setinfo_rice_historymult / 4,
928 (1 << alac->setinfo_rice_kmodifier) - 1);
930 yield();
931 if (prediction_type_a == 0)
932 { /* adaptive fir */
933 predictor_decompress_fir_adapt(outputbuffer[0],
934 outputbuffer[0],
935 outputsamples,
936 readsamplesize,
937 predictor_coef_table_a,
938 predictor_coef_num_a,
939 prediction_quantitization_a);
941 else
942 { /* see mono case */
943 //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_a);
946 yield();
948 /* channel 2 */
949 basterdised_rice_decompress(alac,
950 outputbuffer[1],
951 outputsamples,
952 readsamplesize,
953 alac->setinfo_rice_initialhistory,
954 alac->setinfo_rice_kmodifier,
955 ricemodifier_b * alac->setinfo_rice_historymult / 4,
956 (1 << alac->setinfo_rice_kmodifier) - 1);
958 yield();
959 if (prediction_type_b == 0)
960 { /* adaptive fir */
961 predictor_decompress_fir_adapt(outputbuffer[1],
962 outputbuffer[1],
963 outputsamples,
964 readsamplesize,
965 predictor_coef_table_b,
966 predictor_coef_num_b,
967 prediction_quantitization_b);
969 else
971 //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_b);
974 else
975 { /* not compressed, easy case */
976 if (alac->setinfo_sample_size <= 16)
978 int i;
979 for (i = 0; i < outputsamples; i++)
981 int32_t audiobits_a, audiobits_b;
983 audiobits_a = readbits(alac, alac->setinfo_sample_size);
984 audiobits_b = readbits(alac, alac->setinfo_sample_size);
986 audiobits_a = SIGN_EXTENDED32(audiobits_a, alac->setinfo_sample_size);
987 audiobits_b = SIGN_EXTENDED32(audiobits_b, alac->setinfo_sample_size);
989 outputbuffer[0][i] = audiobits_a;
990 outputbuffer[1][i] = audiobits_b;
993 else
995 int i;
996 for (i = 0; i < outputsamples; i++)
998 int32_t audiobits_a, audiobits_b;
1000 audiobits_a = readbits(alac, 16);
1001 audiobits_a = audiobits_a << 16;
1002 audiobits_a = audiobits_a >> (32 - alac->setinfo_sample_size);
1003 audiobits_a |= readbits(alac, alac->setinfo_sample_size - 16);
1005 audiobits_b = readbits(alac, 16);
1006 audiobits_b = audiobits_b << 16;
1007 audiobits_b = audiobits_b >> (32 - alac->setinfo_sample_size);
1008 audiobits_b |= readbits(alac, alac->setinfo_sample_size - 16);
1010 outputbuffer[0][i] = audiobits_a;
1011 outputbuffer[1][i] = audiobits_b;
1014 /* wasted_bytes = 0; */
1015 interlacing_shift = 0;
1016 interlacing_leftweight = 0;
1019 yield();
1021 switch(alac->setinfo_sample_size)
1023 case 16:
1025 deinterlace_16(outputbuffer[0],
1026 outputbuffer[1],
1027 outputsamples,
1028 interlacing_shift,
1029 interlacing_leftweight);
1030 break;
1032 case 20:
1033 case 24:
1034 case 32:
1035 //fprintf(stderr, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
1036 break;
1037 default:
1038 break;
1040 return outputsamples;
1043 int alac_decode_frame(alac_file *alac,
1044 unsigned char *inbuffer,
1045 int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE],
1046 void (*yield)(void))
1048 int channels;
1049 int outputsamples;
1051 /* setup the stream */
1052 alac->input_buffer = inbuffer;
1053 alac->input_buffer_bitaccumulator = 0;
1055 channels = readbits(alac, 3);
1057 /* TODO: The mono and stereo functions should be combined. */
1058 switch(channels)
1060 case 0: /* 1 channel */
1061 outputsamples=decode_frame_mono(alac,outputbuffer,yield);
1062 break;
1063 case 1: /* 2 channels */
1064 outputsamples=decode_frame_stereo(alac,outputbuffer,yield);
1065 break;
1066 default: /* Unsupported */
1067 return -1;
1069 return outputsamples;
1072 void create_alac(int samplesize, int numchannels, alac_file* alac)
1074 alac->samplesize = samplesize;
1075 alac->numchannels = numchannels;
1076 alac->bytespersample = (samplesize / 8) * numchannels;