Use clz instruction to count leading zeroes on armv5+, gives about 2% speedup with...
[kugel-rb.git] / apps / codecs / libalac / alac.c
blobf94ff0fa9d70e952610df5fb9bada399b2b24d02
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 /* ARMv5+ has a clz instruction equivalent to our function.
171 #if (defined(CPU_ARM) && (ARM_ARCH > 4))
172 static inline int count_leading_zeros(uint32_t v)
174 return __builtin_clz(v);
176 #else
178 static const unsigned char bittab[16] ICONST_ATTR = {
179 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4
182 static inline int count_leading_zeros(int input)
184 int output = 32;
186 #if 0
187 /* Experimentation has shown that the following test is always false,
188 so we don't bother to perform it. */
189 if (input & 0xffff0000)
191 input >>= 16;
192 output -= 16;
194 #endif
195 if (input & 0xff00)
197 input >>= 8;
198 output -= 8;
200 if (input & 0xf0)
202 input >>= 4;
203 output -= 4;
205 output -= bittab[input];
206 return output;
208 #endif
210 void basterdised_rice_decompress(alac_file *alac,
211 int32_t *output_buffer,
212 int output_size,
213 int readsamplesize, /* arg_10 */
214 int rice_initialhistory, /* arg424->b */
215 int rice_kmodifier, /* arg424->d */
216 int rice_historymult, /* arg424->c */
217 int rice_kmodifier_mask /* arg424->e */
218 ) ICODE_ATTR_ALAC;
219 void basterdised_rice_decompress(alac_file *alac,
220 int32_t *output_buffer,
221 int output_size,
222 int readsamplesize, /* arg_10 */
223 int rice_initialhistory, /* arg424->b */
224 int rice_kmodifier, /* arg424->d */
225 int rice_historymult, /* arg424->c */
226 int rice_kmodifier_mask /* arg424->e */
229 int output_count;
230 unsigned int history = rice_initialhistory;
231 int sign_modifier = 0;
233 for (output_count = 0; output_count < output_size; output_count++)
235 int32_t x = 0;
236 int32_t x_modified;
237 int32_t final_val;
239 /* read x - number of 1s before 0 represent the rice */
240 while (x <= 8 && readbit(alac))
242 x++;
246 if (x > 8) /* RICE THRESHOLD */
247 { /* use alternative encoding */
248 int32_t value;
250 value = readbits(alac, readsamplesize);
252 /* mask value to readsamplesize size */
253 if (readsamplesize != 32)
254 value &= (0xffffffff >> (32 - readsamplesize));
256 x = value;
258 else
259 { /* standard rice encoding */
260 int extrabits;
261 int k; /* size of extra bits */
263 /* read k, that is bits as is */
264 k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3);
266 if (k < 0) k += rice_kmodifier;
267 else k = rice_kmodifier;
269 if (k != 1)
271 extrabits = readbits(alac, k);
273 /* multiply x by 2^k - 1, as part of their strange algorithm */
274 x = (x << k) - x;
276 if (extrabits > 1)
278 x += extrabits - 1;
280 else unreadbits(alac, 1);
284 x_modified = sign_modifier + x;
285 final_val = (x_modified + 1) / 2;
286 if (x_modified & 1) final_val *= -1;
288 output_buffer[output_count] = final_val;
290 sign_modifier = 0;
292 /* now update the history */
293 history += (x_modified * rice_historymult)
294 - ((history * rice_historymult) >> 9);
296 if (x_modified > 0xffff)
297 history = 0xffff;
299 /* special case: there may be compressed blocks of 0 */
300 if ((history < 128) && (output_count+1 < output_size))
302 int block_size;
304 sign_modifier = 1;
306 x = 0;
307 while (x <= 8 && readbit(alac))
309 x++;
312 if (x > 8)
314 block_size = readbits(alac, 16);
315 block_size &= 0xffff;
317 else
319 int k;
320 int extrabits;
322 k = count_leading_zeros(history) + ((history + 16) >> 6 /* / 64 */) - 24;
324 extrabits = readbits(alac, k);
326 block_size = (((1 << k) - 1) & rice_kmodifier_mask) * x
327 + extrabits - 1;
329 if (extrabits < 2)
331 x = 1 - extrabits;
332 block_size += x;
333 unreadbits(alac, 1);
337 if (block_size > 0)
339 memset(&output_buffer[output_count+1], 0, block_size * 4);
340 output_count += block_size;
344 if (block_size > 0xffff)
345 sign_modifier = 0;
347 history = 0;
352 #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
354 #define SIGN_ONLY(v) \
355 ((v < 0) ? (-1) : \
356 ((v > 0) ? (1) : \
357 (0)))
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) ICODE_ATTR_ALAC;
366 static void predictor_decompress_fir_adapt(int32_t *error_buffer,
367 int32_t *buffer_out,
368 int output_size,
369 int readsamplesize,
370 int16_t *predictor_coef_table,
371 int predictor_coef_num,
372 int predictor_quantitization)
374 int i;
376 /* first sample always copies */
377 *buffer_out = *error_buffer;
379 if (!predictor_coef_num)
381 if (output_size <= 1) return;
382 memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
383 return;
386 if (predictor_coef_num == 0x1f) /* 11111 - max value of predictor_coef_num */
387 { /* second-best case scenario for fir decompression,
388 * error describes a small difference from the previous sample only
390 if (output_size <= 1) return;
391 for (i = 0; i < output_size - 1; i++)
393 int32_t prev_value;
394 int32_t error_value;
396 prev_value = buffer_out[i];
397 error_value = error_buffer[i+1];
398 buffer_out[i+1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize);
400 return;
403 /* read warm-up samples */
404 if (predictor_coef_num > 0)
406 int i;
407 for (i = 0; i < predictor_coef_num; i++)
409 int32_t val;
411 val = buffer_out[i] + error_buffer[i+1];
413 val = SIGN_EXTENDED32(val, readsamplesize);
415 buffer_out[i+1] = val;
419 /* 4 and 8 are very common cases (the only ones i've seen).
421 The following code is an initial attempt to unroll and optimise
422 these two cases by the Rockbox project. More work is needed.
425 /* optimised case: 4 */
426 if (predictor_coef_num == 4)
428 for (i = 4 + 1; i < output_size; i++)
430 int sum = 0;
431 int outval;
432 int error_val = error_buffer[i];
434 sum = (buffer_out[4] - buffer_out[0]) * predictor_coef_table[0]
435 + (buffer_out[3] - buffer_out[0]) * predictor_coef_table[1]
436 + (buffer_out[2] - buffer_out[0]) * predictor_coef_table[2]
437 + (buffer_out[1] - buffer_out[0]) * predictor_coef_table[3];
439 outval = (1 << (predictor_quantitization-1)) + sum;
440 outval = outval >> predictor_quantitization;
441 outval = outval + buffer_out[0] + error_val;
442 outval = SIGN_EXTENDED32(outval, readsamplesize);
444 buffer_out[4+1] = outval;
446 if (error_val > 0)
448 int predictor_num = 4 - 1;
450 while (predictor_num >= 0 && error_val > 0)
452 int val = buffer_out[0] - buffer_out[4 - predictor_num];
454 if (val!=0) {
455 if (val < 0) {
456 predictor_coef_table[predictor_num]++;
457 val=-val;
458 } else {
459 predictor_coef_table[predictor_num]--;
461 error_val -= ((val >> predictor_quantitization) * (4 - predictor_num));
463 predictor_num--;
466 else if (error_val < 0)
468 int predictor_num = 4 - 1;
470 while (predictor_num >= 0 && error_val < 0)
472 int val = buffer_out[0] - buffer_out[4 - predictor_num];
474 if (val != 0) {
475 if (val > 0) {
476 predictor_coef_table[predictor_num]++;
477 val=-val; /* neg value */
478 } else {
479 predictor_coef_table[predictor_num]--;
481 error_val -= ((val >> predictor_quantitization) * (4 - predictor_num));
483 predictor_num--;
487 buffer_out++;
489 return;
492 /* optimised case: 8 */
493 if (predictor_coef_num == 8)
495 for (i = 8 + 1;
496 i < output_size;
497 i++)
499 int sum;
500 int outval;
501 int error_val = error_buffer[i];
503 sum = (buffer_out[8] - buffer_out[0]) * predictor_coef_table[0]
504 + (buffer_out[7] - buffer_out[0]) * predictor_coef_table[1]
505 + (buffer_out[6] - buffer_out[0]) * predictor_coef_table[2]
506 + (buffer_out[5] - buffer_out[0]) * predictor_coef_table[3]
507 + (buffer_out[4] - buffer_out[0]) * predictor_coef_table[4]
508 + (buffer_out[3] - buffer_out[0]) * predictor_coef_table[5]
509 + (buffer_out[2] - buffer_out[0]) * predictor_coef_table[6]
510 + (buffer_out[1] - buffer_out[0]) * predictor_coef_table[7];
512 outval = (1 << (predictor_quantitization-1)) + sum;
513 outval = outval >> predictor_quantitization;
514 outval = outval + buffer_out[0] + error_val;
515 outval = SIGN_EXTENDED32(outval, readsamplesize);
517 buffer_out[8+1] = outval;
519 if (error_val > 0)
521 int predictor_num = 8 - 1;
523 while (predictor_num >= 0 && error_val > 0)
525 int val = buffer_out[0] - buffer_out[8 - predictor_num];
527 if (val!=0) {
528 if (val < 0) {
529 predictor_coef_table[predictor_num]++;
530 val=-val;
531 } else {
532 predictor_coef_table[predictor_num]--;
534 error_val -= ((val >> predictor_quantitization) * (8 - predictor_num));
536 predictor_num--;
539 else if (error_val < 0)
541 int predictor_num = 8 - 1;
543 while (predictor_num >= 0 && error_val < 0)
545 int val = buffer_out[0] - buffer_out[8 - predictor_num];
546 if (val != 0) {
547 if (val > 0) {
548 predictor_coef_table[predictor_num]++;
549 val=-val; /* neg value */
550 } else {
551 predictor_coef_table[predictor_num]--;
553 error_val -= ((val >> predictor_quantitization) * (8 - predictor_num));
555 predictor_num--;
559 buffer_out++;
561 return;
564 /* general case */
565 if (predictor_coef_num > 0)
567 for (i = predictor_coef_num + 1;
568 i < output_size;
569 i++)
571 int j;
572 int sum = 0;
573 int outval;
574 int error_val = error_buffer[i];
576 for (j = 0; j < predictor_coef_num; j++)
578 sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
579 predictor_coef_table[j];
582 outval = (1 << (predictor_quantitization-1)) + sum;
583 outval = outval >> predictor_quantitization;
584 outval = outval + buffer_out[0] + error_val;
585 outval = SIGN_EXTENDED32(outval, readsamplesize);
587 buffer_out[predictor_coef_num+1] = outval;
589 if (error_val > 0)
591 int predictor_num = predictor_coef_num - 1;
593 while (predictor_num >= 0 && error_val > 0)
595 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
596 int sign = SIGN_ONLY(val);
598 predictor_coef_table[predictor_num] -= sign;
600 val *= sign; /* absolute value */
602 error_val -= ((val >> predictor_quantitization) *
603 (predictor_coef_num - predictor_num));
605 predictor_num--;
608 else if (error_val < 0)
610 int predictor_num = predictor_coef_num - 1;
612 while (predictor_num >= 0 && error_val < 0)
614 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
615 int sign = - SIGN_ONLY(val);
617 predictor_coef_table[predictor_num] -= sign;
619 val *= sign; /* neg value */
621 error_val -= ((val >> predictor_quantitization) *
622 (predictor_coef_num - predictor_num));
624 predictor_num--;
628 buffer_out++;
633 void deinterlace_16(int32_t* buffer0,
634 int32_t* buffer1,
635 int numsamples,
636 uint8_t interlacing_shift,
637 uint8_t interlacing_leftweight) ICODE_ATTR_ALAC;
638 void deinterlace_16(int32_t* buffer0,
639 int32_t* buffer1,
640 int numsamples,
641 uint8_t interlacing_shift,
642 uint8_t interlacing_leftweight)
644 int i;
645 if (numsamples <= 0) return;
647 /* weighted interlacing */
648 if (interlacing_leftweight)
650 for (i = 0; i < numsamples; i++)
652 int32_t difference, midright;
654 midright = buffer0[i];
655 difference = buffer1[i];
657 buffer0[i] = ((midright - ((difference * interlacing_leftweight)
658 >> interlacing_shift)) + difference) << SCALE16;
659 buffer1[i] = (midright - ((difference * interlacing_leftweight)
660 >> interlacing_shift)) << SCALE16;
663 return;
666 /* otherwise basic interlacing took place */
667 for (i = 0; i < numsamples; i++)
669 buffer0[i] = buffer0[i] << SCALE16;
670 buffer1[i] = buffer1[i] << SCALE16;
675 static inline int decode_frame_mono(
676 alac_file *alac,
677 int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE],
678 void (*yield)(void))
680 int hassize;
681 int isnotcompressed;
682 int readsamplesize;
683 int outputsamples = alac->setinfo_max_samples_per_frame;
685 int wasted_bytes;
686 int ricemodifier;
689 /* 2^result = something to do with output waiting.
690 * perhaps matters if we read > 1 frame in a pass?
692 readbits(alac, 4);
694 readbits(alac, 12); /* unknown, skip 12 bits */
696 hassize = readbits(alac, 1); /* the output sample size is stored soon */
698 wasted_bytes = readbits(alac, 2); /* unknown ? */
700 isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
702 if (hassize)
704 /* now read the number of samples,
705 * as a 32bit integer */
706 outputsamples = readbits(alac, 32);
709 readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8);
711 if (!isnotcompressed)
712 { /* so it is compressed */
713 int predictor_coef_num;
714 int prediction_type;
715 int prediction_quantitization;
716 int i;
718 /* skip 16 bits, not sure what they are. seem to be used in
719 * two channel case */
720 readbits(alac, 8);
721 readbits(alac, 8);
723 prediction_type = readbits(alac, 4);
724 prediction_quantitization = readbits(alac, 4);
726 ricemodifier = readbits(alac, 3);
727 predictor_coef_num = readbits(alac, 5);
729 /* read the predictor table */
730 for (i = 0; i < predictor_coef_num; i++)
732 predictor_coef_table[i] = (int16_t)readbits(alac, 16);
735 if (wasted_bytes)
737 /* these bytes seem to have something to do with
738 * > 2 channel files.
740 //fprintf(stderr, "FIXME: unimplemented, unhandling of wasted_bytes\n");
743 yield();
745 basterdised_rice_decompress(alac,
746 outputbuffer[0],
747 outputsamples,
748 readsamplesize,
749 alac->setinfo_rice_initialhistory,
750 alac->setinfo_rice_kmodifier,
751 ricemodifier * alac->setinfo_rice_historymult / 4,
752 (1 << alac->setinfo_rice_kmodifier) - 1);
754 yield();
756 if (prediction_type == 0)
757 { /* adaptive fir */
758 predictor_decompress_fir_adapt(outputbuffer[0],
759 outputbuffer[0],
760 outputsamples,
761 readsamplesize,
762 predictor_coef_table,
763 predictor_coef_num,
764 prediction_quantitization);
766 else
768 //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type);
769 /* i think the only other prediction type (or perhaps this is just a
770 * boolean?) runs adaptive fir twice.. like:
771 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
772 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
773 * little strange..
778 else
779 { /* not compressed, easy case */
780 if (readsamplesize <= 16)
782 int i;
783 for (i = 0; i < outputsamples; i++)
785 int32_t audiobits = readbits(alac, readsamplesize);
787 audiobits = SIGN_EXTENDED32(audiobits, readsamplesize);
789 outputbuffer[0][i] = audiobits;
792 else
794 int i;
795 for (i = 0; i < outputsamples; i++)
797 int32_t audiobits;
799 audiobits = readbits(alac, 16);
800 /* special case of sign extension..
801 * as we'll be ORing the low 16bits into this */
802 audiobits = audiobits << 16;
803 audiobits = audiobits >> (32 - readsamplesize);
805 audiobits |= readbits(alac, readsamplesize - 16);
807 outputbuffer[0][i] = audiobits;
810 /* wasted_bytes = 0; // unused */
813 yield();
815 switch(alac->setinfo_sample_size)
817 case 16:
819 int i;
820 for (i = 0; i < outputsamples; i++)
822 /* Output mono data as stereo */
823 outputbuffer[0][i] = outputbuffer[0][i] << SCALE16;
824 outputbuffer[1][i] = outputbuffer[0][i];
826 break;
828 case 20:
829 case 24:
830 case 32:
831 //fprintf(stderr, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
832 break;
833 default:
834 break;
837 return outputsamples;
840 static inline int decode_frame_stereo(
841 alac_file *alac,
842 int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE],
843 void (*yield)(void))
845 int hassize;
846 int isnotcompressed;
847 int readsamplesize;
848 int outputsamples = alac->setinfo_max_samples_per_frame;
849 int wasted_bytes;
851 uint8_t interlacing_shift;
852 uint8_t interlacing_leftweight;
854 /* 2^result = something to do with output waiting.
855 * perhaps matters if we read > 1 frame in a pass?
857 readbits(alac, 4);
859 readbits(alac, 12); /* unknown, skip 12 bits */
861 hassize = readbits(alac, 1); /* the output sample size is stored soon */
863 wasted_bytes = readbits(alac, 2); /* unknown ? */
865 isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
867 if (hassize)
869 /* now read the number of samples,
870 * as a 32bit integer */
871 outputsamples = readbits(alac, 32);
874 readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8) + 1;
876 yield();
877 if (!isnotcompressed)
878 { /* compressed */
879 int predictor_coef_num_a;
880 int prediction_type_a;
881 int prediction_quantitization_a;
882 int ricemodifier_a;
884 int predictor_coef_num_b;
885 int prediction_type_b;
886 int prediction_quantitization_b;
887 int ricemodifier_b;
889 int i;
891 interlacing_shift = readbits(alac, 8);
892 interlacing_leftweight = readbits(alac, 8);
894 /******** channel 1 ***********/
895 prediction_type_a = readbits(alac, 4);
896 prediction_quantitization_a = readbits(alac, 4);
898 ricemodifier_a = readbits(alac, 3);
899 predictor_coef_num_a = readbits(alac, 5);
901 /* read the predictor table */
902 for (i = 0; i < predictor_coef_num_a; i++)
904 predictor_coef_table_a[i] = (int16_t)readbits(alac, 16);
907 /******** channel 2 *********/
908 prediction_type_b = readbits(alac, 4);
909 prediction_quantitization_b = readbits(alac, 4);
911 ricemodifier_b = readbits(alac, 3);
912 predictor_coef_num_b = readbits(alac, 5);
914 /* read the predictor table */
915 for (i = 0; i < predictor_coef_num_b; i++)
917 predictor_coef_table_b[i] = (int16_t)readbits(alac, 16);
920 /*********************/
921 if (wasted_bytes)
922 { /* see mono case */
923 //fprintf(stderr, "FIXME: unimplemented, unhandling of wasted_bytes\n");
926 yield();
927 /* channel 1 */
928 basterdised_rice_decompress(alac,
929 outputbuffer[0],
930 outputsamples,
931 readsamplesize,
932 alac->setinfo_rice_initialhistory,
933 alac->setinfo_rice_kmodifier,
934 ricemodifier_a * alac->setinfo_rice_historymult / 4,
935 (1 << alac->setinfo_rice_kmodifier) - 1);
937 yield();
938 if (prediction_type_a == 0)
939 { /* adaptive fir */
940 predictor_decompress_fir_adapt(outputbuffer[0],
941 outputbuffer[0],
942 outputsamples,
943 readsamplesize,
944 predictor_coef_table_a,
945 predictor_coef_num_a,
946 prediction_quantitization_a);
948 else
949 { /* see mono case */
950 //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_a);
953 yield();
955 /* channel 2 */
956 basterdised_rice_decompress(alac,
957 outputbuffer[1],
958 outputsamples,
959 readsamplesize,
960 alac->setinfo_rice_initialhistory,
961 alac->setinfo_rice_kmodifier,
962 ricemodifier_b * alac->setinfo_rice_historymult / 4,
963 (1 << alac->setinfo_rice_kmodifier) - 1);
965 yield();
966 if (prediction_type_b == 0)
967 { /* adaptive fir */
968 predictor_decompress_fir_adapt(outputbuffer[1],
969 outputbuffer[1],
970 outputsamples,
971 readsamplesize,
972 predictor_coef_table_b,
973 predictor_coef_num_b,
974 prediction_quantitization_b);
976 else
978 //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_b);
981 else
982 { /* not compressed, easy case */
983 if (alac->setinfo_sample_size <= 16)
985 int i;
986 for (i = 0; i < outputsamples; i++)
988 int32_t audiobits_a, audiobits_b;
990 audiobits_a = readbits(alac, alac->setinfo_sample_size);
991 audiobits_b = readbits(alac, alac->setinfo_sample_size);
993 audiobits_a = SIGN_EXTENDED32(audiobits_a, alac->setinfo_sample_size);
994 audiobits_b = SIGN_EXTENDED32(audiobits_b, alac->setinfo_sample_size);
996 outputbuffer[0][i] = audiobits_a;
997 outputbuffer[1][i] = audiobits_b;
1000 else
1002 int i;
1003 for (i = 0; i < outputsamples; i++)
1005 int32_t audiobits_a, audiobits_b;
1007 audiobits_a = readbits(alac, 16);
1008 audiobits_a = audiobits_a << 16;
1009 audiobits_a = audiobits_a >> (32 - alac->setinfo_sample_size);
1010 audiobits_a |= readbits(alac, alac->setinfo_sample_size - 16);
1012 audiobits_b = readbits(alac, 16);
1013 audiobits_b = audiobits_b << 16;
1014 audiobits_b = audiobits_b >> (32 - alac->setinfo_sample_size);
1015 audiobits_b |= readbits(alac, alac->setinfo_sample_size - 16);
1017 outputbuffer[0][i] = audiobits_a;
1018 outputbuffer[1][i] = audiobits_b;
1021 /* wasted_bytes = 0; */
1022 interlacing_shift = 0;
1023 interlacing_leftweight = 0;
1026 yield();
1028 switch(alac->setinfo_sample_size)
1030 case 16:
1032 deinterlace_16(outputbuffer[0],
1033 outputbuffer[1],
1034 outputsamples,
1035 interlacing_shift,
1036 interlacing_leftweight);
1037 break;
1039 case 20:
1040 case 24:
1041 case 32:
1042 //fprintf(stderr, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
1043 break;
1044 default:
1045 break;
1047 return outputsamples;
1050 int alac_decode_frame(alac_file *alac,
1051 unsigned char *inbuffer,
1052 int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE],
1053 void (*yield)(void))
1055 int channels;
1056 int outputsamples;
1058 /* setup the stream */
1059 alac->input_buffer = inbuffer;
1060 alac->input_buffer_bitaccumulator = 0;
1062 channels = readbits(alac, 3);
1064 /* TODO: The mono and stereo functions should be combined. */
1065 switch(channels)
1067 case 0: /* 1 channel */
1068 outputsamples=decode_frame_mono(alac,outputbuffer,yield);
1069 break;
1070 case 1: /* 2 channels */
1071 outputsamples=decode_frame_stereo(alac,outputbuffer,yield);
1072 break;
1073 default: /* Unsupported */
1074 return -1;
1076 return outputsamples;
1079 void create_alac(int samplesize, int numchannels, alac_file* alac)
1081 alac->samplesize = samplesize;
1082 alac->numchannels = numchannels;
1083 alac->bytespersample = (samplesize / 8) * numchannels;