Update the discussion of themeing in the manual, and put a note in the wps tags appen...
[kugel-rb.git] / apps / codecs / libalac / alac.c
bloba7d7448b978a4b499aef33252985e0eb2aec304b
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 #define SIGNEXTEND24(val) (((signed)val<<8)>>8)
43 int16_t predictor_coef_table[32] IBSS_ATTR;
44 int16_t predictor_coef_table_a[32] IBSS_ATTR;
45 int16_t predictor_coef_table_b[32] IBSS_ATTR;
48 /* Endian/aligment safe functions - only used in alac_set_info() */
49 static uint32_t get_uint32be(unsigned char* p)
51 return((p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]);
54 static uint16_t get_uint16be(unsigned char* p)
56 return((p[0]<<8) | p[1]);
59 void alac_set_info(alac_file *alac, char *inputbuffer)
61 unsigned char* ptr = (unsigned char*)inputbuffer;
62 ptr += 4; /* size */
63 ptr += 4; /* frma */
64 ptr += 4; /* alac */
65 ptr += 4; /* size */
66 ptr += 4; /* alac */
68 ptr += 4; /* 0 ? */
70 alac->setinfo_max_samples_per_frame = get_uint32be(ptr); /* buffer size / 2 ? */
71 ptr += 4;
72 alac->setinfo_7a = *ptr++;
73 alac->setinfo_sample_size = *ptr++;
74 alac->setinfo_rice_historymult = *ptr++;
75 alac->setinfo_rice_initialhistory = *ptr++;
76 alac->setinfo_rice_kmodifier = *ptr++;
77 alac->setinfo_7f = *ptr++;
78 ptr += 1;
79 alac->setinfo_80 = get_uint16be(ptr);
80 ptr += 2;
81 alac->setinfo_82 = get_uint32be(ptr);
82 ptr += 4;
83 alac->setinfo_86 = get_uint32be(ptr);
84 ptr += 4;
85 alac->setinfo_8a_rate = get_uint32be(ptr);
86 ptr += 4;
89 /* stream reading */
91 /* supports reading 1 to 16 bits, in big endian format */
92 static inline uint32_t readbits_16(alac_file *alac, int bits)
94 uint32_t result;
95 int new_accumulator;
97 result = (alac->input_buffer[0] << 16) |
98 (alac->input_buffer[1] << 8) |
99 (alac->input_buffer[2]);
101 /* shift left by the number of bits we've already read,
102 * so that the top 'n' bits of the 24 bits we read will
103 * be the return bits */
104 result = result << alac->input_buffer_bitaccumulator;
106 result = result & 0x00ffffff;
108 /* and then only want the top 'n' bits from that, where
109 * n is 'bits' */
110 result = result >> (24 - bits);
112 new_accumulator = (alac->input_buffer_bitaccumulator + bits);
114 /* increase the buffer pointer if we've read over n bytes. */
115 alac->input_buffer += (new_accumulator >> 3);
117 /* and the remainder goes back into the bit accumulator */
118 alac->input_buffer_bitaccumulator = (new_accumulator & 7);
120 return result;
123 /* supports reading 1 to 32 bits, in big endian format */
124 static inline uint32_t readbits(alac_file *alac, int bits)
126 int32_t result = 0;
128 if (bits > 16)
130 bits -= 16;
131 result = readbits_16(alac, 16) << bits;
134 result |= readbits_16(alac, bits);
136 return result;
139 /* reads a single bit */
140 static inline int readbit(alac_file *alac)
142 int result;
143 int new_accumulator;
145 result = alac->input_buffer[0];
147 result = result << alac->input_buffer_bitaccumulator;
149 result = result >> 7 & 1;
151 new_accumulator = (alac->input_buffer_bitaccumulator + 1);
153 alac->input_buffer += (new_accumulator / 8);
155 alac->input_buffer_bitaccumulator = (new_accumulator % 8);
157 return result;
160 static inline void unreadbits(alac_file *alac, int bits)
162 int new_accumulator = (alac->input_buffer_bitaccumulator - bits);
164 alac->input_buffer += (new_accumulator >> 3);
166 alac->input_buffer_bitaccumulator = (new_accumulator & 7);
167 if (alac->input_buffer_bitaccumulator < 0)
168 alac->input_buffer_bitaccumulator *= -1;
171 #define count_leading_zeros(x) bs_generic(x, BS_CLZ|BS_SHORT)
173 #define RICE_THRESHOLD 8 // maximum number of bits for a rice prefix.
175 static inline int32_t entropy_decode_value(alac_file* alac,
176 int readsamplesize,
177 int k) ICODE_ATTR_ALAC;
178 static inline int32_t entropy_decode_value(alac_file* alac,
179 int readsamplesize,
180 int k)
182 int32_t x = 0; // decoded value
184 // read x, number of 1s before 0 represent the rice value.
185 while (x <= RICE_THRESHOLD && readbit(alac))
187 x++;
190 if (x > RICE_THRESHOLD)
192 // read the number from the bit stream (raw value)
193 int32_t value;
195 value = readbits(alac, readsamplesize);
197 /* mask value to readsamplesize size */
198 if (readsamplesize != 32)
199 value &= (((uint32_t)0xffffffff) >> (32 - readsamplesize));
201 x = value;
203 else
205 if (k != 1)
207 int extrabits = readbits(alac, k);
209 // x = x * (2^k - 1)
210 x = (x << k) - x;
212 if (extrabits > 1)
213 x += extrabits - 1;
214 else
215 unreadbits(alac, 1);
219 return x;
222 static void entropy_rice_decode(alac_file* alac,
223 int32_t* output_buffer,
224 int output_size,
225 int readsamplesize,
226 int rice_initialhistory,
227 int rice_kmodifier,
228 int rice_historymult,
229 int rice_kmodifier_mask) ICODE_ATTR_ALAC;
230 static void entropy_rice_decode(alac_file* alac,
231 int32_t* output_buffer,
232 int output_size,
233 int readsamplesize,
234 int rice_initialhistory,
235 int rice_kmodifier,
236 int rice_historymult,
237 int rice_kmodifier_mask)
239 int output_count;
240 int history = rice_initialhistory;
241 int sign_modifier = 0;
243 for (output_count = 0; output_count < output_size; output_count++)
245 int32_t decoded_value;
246 int32_t final_value;
247 int32_t k;
249 k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3);
251 if (k < 0) k += rice_kmodifier;
252 else k = rice_kmodifier;
254 decoded_value = entropy_decode_value(alac, readsamplesize, k);
256 decoded_value += sign_modifier;
257 final_value = (decoded_value + 1) / 2; // inc by 1 and shift out sign bit
258 if (decoded_value & 1) // the sign is stored in the low bit
259 final_value *= -1;
261 output_buffer[output_count] = final_value;
263 sign_modifier = 0;
265 // update history
266 history += (decoded_value * rice_historymult)
267 - ((history * rice_historymult) >> 9);
269 if (decoded_value > 0xFFFF)
270 history = 0xFFFF;
272 // special case, for compressed blocks of 0
273 if ((history < 128) && (output_count + 1 < output_size))
275 int32_t block_size;
277 sign_modifier = 1;
279 k = count_leading_zeros(history) + ((history + 16) / 64) - 24;
281 // note: block_size is always 16bit
282 block_size = entropy_decode_value(alac, 16, k) & rice_kmodifier_mask;
284 // got block_size 0s
285 if (block_size > 0)
287 memset(&output_buffer[output_count + 1], 0,
288 block_size * sizeof(*output_buffer));
289 output_count += block_size;
292 if (block_size > 0xFFFF)
293 sign_modifier = 0;
295 history = 0;
300 #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
302 #define SIGN_ONLY(v) \
303 ((v < 0) ? (-1) : \
304 ((v > 0) ? (1) : \
305 (0)))
307 static void predictor_decompress_fir_adapt(int32_t *error_buffer,
308 int32_t *buffer_out,
309 int output_size,
310 int readsamplesize,
311 int16_t *predictor_coef_table,
312 int predictor_coef_num,
313 int predictor_quantitization) ICODE_ATTR_ALAC;
314 static void predictor_decompress_fir_adapt(int32_t *error_buffer,
315 int32_t *buffer_out,
316 int output_size,
317 int readsamplesize,
318 int16_t *predictor_coef_table,
319 int predictor_coef_num,
320 int predictor_quantitization)
322 int i;
324 /* first sample always copies */
325 *buffer_out = *error_buffer;
327 if (!predictor_coef_num)
329 if (output_size <= 1) return;
330 memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
331 return;
334 if (predictor_coef_num == 0x1f) /* 11111 - max value of predictor_coef_num */
335 { /* second-best case scenario for fir decompression,
336 * error describes a small difference from the previous sample only
338 if (output_size <= 1) return;
339 for (i = 0; i < output_size - 1; i++)
341 int32_t prev_value;
342 int32_t error_value;
344 prev_value = buffer_out[i];
345 error_value = error_buffer[i+1];
346 buffer_out[i+1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize);
348 return;
351 /* read warm-up samples */
352 if (predictor_coef_num > 0)
354 int i;
355 for (i = 0; i < predictor_coef_num; i++)
357 int32_t val;
359 val = buffer_out[i] + error_buffer[i+1];
361 val = SIGN_EXTENDED32(val, readsamplesize);
363 buffer_out[i+1] = val;
367 /* 4 and 8 are very common cases (the only ones i've seen).
369 The following code is an initial attempt to unroll and optimise
370 these two cases by the Rockbox project. More work is needed.
373 /* optimised case: 4 */
374 if (predictor_coef_num == 4)
376 for (i = 4 + 1; i < output_size; i++)
378 int sum = 0;
379 int outval;
380 int error_val = error_buffer[i];
382 sum = (buffer_out[4] - buffer_out[0]) * predictor_coef_table[0]
383 + (buffer_out[3] - buffer_out[0]) * predictor_coef_table[1]
384 + (buffer_out[2] - buffer_out[0]) * predictor_coef_table[2]
385 + (buffer_out[1] - buffer_out[0]) * predictor_coef_table[3];
387 outval = (1 << (predictor_quantitization-1)) + sum;
388 outval = outval >> predictor_quantitization;
389 outval = outval + buffer_out[0] + error_val;
390 outval = SIGN_EXTENDED32(outval, readsamplesize);
392 buffer_out[4+1] = outval;
394 if (error_val > 0)
396 int predictor_num = 4 - 1;
398 while (predictor_num >= 0 && error_val > 0)
400 int val = buffer_out[0] - buffer_out[4 - predictor_num];
402 if (val!=0) {
403 if (val < 0) {
404 predictor_coef_table[predictor_num]++;
405 val=-val;
406 } else {
407 predictor_coef_table[predictor_num]--;
409 error_val -= ((val >> predictor_quantitization) * (4 - predictor_num));
411 predictor_num--;
414 else if (error_val < 0)
416 int predictor_num = 4 - 1;
418 while (predictor_num >= 0 && error_val < 0)
420 int val = buffer_out[0] - buffer_out[4 - predictor_num];
422 if (val != 0) {
423 if (val > 0) {
424 predictor_coef_table[predictor_num]++;
425 val=-val; /* neg value */
426 } else {
427 predictor_coef_table[predictor_num]--;
429 error_val -= ((val >> predictor_quantitization) * (4 - predictor_num));
431 predictor_num--;
435 buffer_out++;
437 return;
440 /* optimised case: 8 */
441 if (predictor_coef_num == 8)
443 for (i = 8 + 1;
444 i < output_size;
445 i++)
447 int sum;
448 int outval;
449 int error_val = error_buffer[i];
451 sum = (buffer_out[8] - buffer_out[0]) * predictor_coef_table[0]
452 + (buffer_out[7] - buffer_out[0]) * predictor_coef_table[1]
453 + (buffer_out[6] - buffer_out[0]) * predictor_coef_table[2]
454 + (buffer_out[5] - buffer_out[0]) * predictor_coef_table[3]
455 + (buffer_out[4] - buffer_out[0]) * predictor_coef_table[4]
456 + (buffer_out[3] - buffer_out[0]) * predictor_coef_table[5]
457 + (buffer_out[2] - buffer_out[0]) * predictor_coef_table[6]
458 + (buffer_out[1] - buffer_out[0]) * predictor_coef_table[7];
460 outval = (1 << (predictor_quantitization-1)) + sum;
461 outval = outval >> predictor_quantitization;
462 outval = outval + buffer_out[0] + error_val;
463 outval = SIGN_EXTENDED32(outval, readsamplesize);
465 buffer_out[8+1] = outval;
467 if (error_val > 0)
469 int predictor_num = 8 - 1;
471 while (predictor_num >= 0 && error_val > 0)
473 int val = buffer_out[0] - buffer_out[8 - predictor_num];
475 if (val!=0) {
476 if (val < 0) {
477 predictor_coef_table[predictor_num]++;
478 val=-val;
479 } else {
480 predictor_coef_table[predictor_num]--;
482 error_val -= ((val >> predictor_quantitization) * (8 - predictor_num));
484 predictor_num--;
487 else if (error_val < 0)
489 int predictor_num = 8 - 1;
491 while (predictor_num >= 0 && error_val < 0)
493 int val = buffer_out[0] - buffer_out[8 - predictor_num];
494 if (val != 0) {
495 if (val > 0) {
496 predictor_coef_table[predictor_num]++;
497 val=-val; /* neg value */
498 } else {
499 predictor_coef_table[predictor_num]--;
501 error_val -= ((val >> predictor_quantitization) * (8 - predictor_num));
503 predictor_num--;
507 buffer_out++;
509 return;
512 /* general case */
513 if (predictor_coef_num > 0)
515 for (i = predictor_coef_num + 1;
516 i < output_size;
517 i++)
519 int j;
520 int sum = 0;
521 int outval;
522 int error_val = error_buffer[i];
524 for (j = 0; j < predictor_coef_num; j++)
526 sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
527 predictor_coef_table[j];
530 outval = (1 << (predictor_quantitization-1)) + sum;
531 outval = outval >> predictor_quantitization;
532 outval = outval + buffer_out[0] + error_val;
533 outval = SIGN_EXTENDED32(outval, readsamplesize);
535 buffer_out[predictor_coef_num+1] = outval;
537 if (error_val > 0)
539 int predictor_num = predictor_coef_num - 1;
541 while (predictor_num >= 0 && error_val > 0)
543 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
544 int sign = SIGN_ONLY(val);
546 predictor_coef_table[predictor_num] -= sign;
548 val *= sign; /* absolute value */
550 error_val -= ((val >> predictor_quantitization) *
551 (predictor_coef_num - predictor_num));
553 predictor_num--;
556 else if (error_val < 0)
558 int predictor_num = predictor_coef_num - 1;
560 while (predictor_num >= 0 && error_val < 0)
562 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
563 int sign = - SIGN_ONLY(val);
565 predictor_coef_table[predictor_num] -= sign;
567 val *= sign; /* neg value */
569 error_val -= ((val >> predictor_quantitization) *
570 (predictor_coef_num - predictor_num));
572 predictor_num--;
576 buffer_out++;
581 void deinterlace_16(int32_t* buffer0,
582 int32_t* buffer1,
583 int numsamples,
584 uint8_t interlacing_shift,
585 uint8_t interlacing_leftweight) ICODE_ATTR_ALAC;
586 void deinterlace_16(int32_t* buffer0,
587 int32_t* buffer1,
588 int numsamples,
589 uint8_t interlacing_shift,
590 uint8_t interlacing_leftweight)
592 int i;
593 if (numsamples <= 0) return;
595 /* weighted interlacing */
596 if (interlacing_leftweight)
598 for (i = 0; i < numsamples; i++)
600 int32_t difference, midright;
602 midright = buffer0[i];
603 difference = buffer1[i];
605 buffer0[i] = ((midright - ((difference * interlacing_leftweight)
606 >> interlacing_shift)) + difference) << SCALE16;
607 buffer1[i] = (midright - ((difference * interlacing_leftweight)
608 >> interlacing_shift)) << SCALE16;
611 return;
614 /* otherwise basic interlacing took place */
615 for (i = 0; i < numsamples; i++)
617 buffer0[i] = buffer0[i] << SCALE16;
618 buffer1[i] = buffer1[i] << SCALE16;
622 void deinterlace_24(int32_t *buffer0, int32_t *buffer1,
623 int uncompressed_bytes,
624 int32_t *uncompressed_bytes_buffer0,
625 int32_t *uncompressed_bytes_buffer1,
626 int numsamples,
627 uint8_t interlacing_shift,
628 uint8_t interlacing_leftweight) ICODE_ATTR_ALAC;
629 void deinterlace_24(int32_t *buffer0, int32_t *buffer1,
630 int uncompressed_bytes,
631 int32_t *uncompressed_bytes_buffer0,
632 int32_t *uncompressed_bytes_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) << SCALE24;
652 buffer1[i] = (midright - ((difference * interlacing_leftweight)
653 >> interlacing_shift)) << SCALE24;
655 if (uncompressed_bytes)
657 uint32_t mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8));
658 buffer0[i] <<= (uncompressed_bytes * 8);
659 buffer1[i] <<= (uncompressed_bytes * 8);
661 buffer0[i] |= uncompressed_bytes_buffer0[i] & mask;
662 buffer1[i] |= uncompressed_bytes_buffer1[i] & mask;
667 return;
670 /* otherwise basic interlacing took place */
671 for (i = 0; i < numsamples; i++)
673 if (uncompressed_bytes)
675 uint32_t mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8));
676 buffer0[i] <<= (uncompressed_bytes * 8);
677 buffer1[i] <<= (uncompressed_bytes * 8);
679 buffer0[i] |= uncompressed_bytes_buffer0[i] & mask;
680 buffer1[i] |= uncompressed_bytes_buffer1[i] & mask;
683 buffer0[i] = buffer0[i] << SCALE24;
684 buffer1[i] = buffer1[i] << SCALE24;
689 static inline int decode_frame_mono(
690 alac_file *alac,
691 int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE],
692 void (*yield)(void))
694 int hassize;
695 int isnotcompressed;
696 int readsamplesize;
697 int infosamplesize = alac->setinfo_sample_size;
698 int outputsamples = alac->setinfo_max_samples_per_frame;
700 int uncompressed_bytes;
701 int ricemodifier;
704 /* 2^result = something to do with output waiting.
705 * perhaps matters if we read > 1 frame in a pass?
707 readbits(alac, 4);
709 readbits(alac, 12); /* unknown, skip 12 bits */
711 hassize = readbits(alac, 1); /* the output sample size is stored soon */
713 /* number of bytes in the (compressed) stream that are not compressed */
714 uncompressed_bytes = readbits(alac, 2);
716 isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
718 if (hassize)
720 /* now read the number of samples,
721 * as a 32bit integer */
722 outputsamples = readbits(alac, 32);
725 readsamplesize = infosamplesize - (uncompressed_bytes * 8);
727 if (!isnotcompressed)
728 { /* so it is compressed */
729 int predictor_coef_num;
730 int prediction_type;
731 int prediction_quantitization;
732 int i;
734 /* skip 16 bits, not sure what they are. seem to be used in
735 * two channel case */
736 readbits(alac, 8);
737 readbits(alac, 8);
739 prediction_type = readbits(alac, 4);
740 prediction_quantitization = readbits(alac, 4);
742 ricemodifier = readbits(alac, 3);
743 predictor_coef_num = readbits(alac, 5);
745 /* read the predictor table */
746 for (i = 0; i < predictor_coef_num; i++)
748 predictor_coef_table[i] = (int16_t)readbits(alac, 16);
751 if (uncompressed_bytes)
753 int i;
754 for (i = 0; i < outputsamples; i++)
756 outputbuffer[0][i] = readbits(alac, uncompressed_bytes * 8);
757 outputbuffer[1][i] = outputbuffer[0][i];
761 yield();
763 entropy_rice_decode(alac,
764 outputbuffer[0],
765 outputsamples,
766 readsamplesize,
767 alac->setinfo_rice_initialhistory,
768 alac->setinfo_rice_kmodifier,
769 ricemodifier * alac->setinfo_rice_historymult / 4,
770 (1 << alac->setinfo_rice_kmodifier) - 1);
772 yield();
774 if (prediction_type == 0)
775 { /* adaptive fir */
776 predictor_decompress_fir_adapt(outputbuffer[0],
777 outputbuffer[0],
778 outputsamples,
779 readsamplesize,
780 predictor_coef_table,
781 predictor_coef_num,
782 prediction_quantitization);
784 else
786 //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type);
787 /* i think the only other prediction type (or perhaps this is just a
788 * boolean?) runs adaptive fir twice.. like:
789 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
790 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
791 * little strange..
796 else
797 { /* not compressed, easy case */
798 if (infosamplesize <= 16)
800 int i;
801 for (i = 0; i < outputsamples; i++)
803 int32_t audiobits = readbits(alac, infosamplesize);
805 audiobits = SIGN_EXTENDED32(audiobits, infosamplesize);
807 outputbuffer[0][i] = audiobits;
810 else
812 int i;
813 for (i = 0; i < outputsamples; i++)
815 int32_t audiobits;
817 audiobits = readbits(alac, 16);
818 /* special case of sign extension..
819 * as we'll be ORing the low 16bits into this */
820 audiobits = audiobits << (infosamplesize - 16);
821 audiobits |= readbits(alac, infosamplesize - 16);
822 audiobits = SIGNEXTEND24(audiobits);
824 outputbuffer[0][i] = audiobits;
827 uncompressed_bytes = 0; // always 0 for uncompressed
830 yield();
832 switch(infosamplesize)
834 case 16:
836 int i;
837 for (i = 0; i < outputsamples; i++)
839 /* Output mono data as stereo */
840 outputbuffer[0][i] = outputbuffer[0][i] << SCALE16;
841 outputbuffer[1][i] = outputbuffer[0][i];
843 break;
845 case 24:
847 int i;
848 for (i = 0; i < outputsamples; i++)
850 int32_t sample = outputbuffer[0][i];
852 if (uncompressed_bytes)
854 uint32_t mask;
855 sample = sample << (uncompressed_bytes * 8);
856 mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8));
857 sample |= outputbuffer[0][i] & mask;
860 outputbuffer[0][i] = sample << SCALE24;
861 outputbuffer[1][i] = outputbuffer[0][i];
863 break;
865 case 20:
866 case 32:
867 //fprintf(stderr, "FIXME: unimplemented sample size %i\n", infosamplesize);
868 break;
869 default:
870 break;
873 return outputsamples;
876 static inline int decode_frame_stereo(
877 alac_file *alac,
878 int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE],
879 void (*yield)(void))
881 int hassize;
882 int isnotcompressed;
883 int readsamplesize;
884 int infosamplesize = alac->setinfo_sample_size;
885 int outputsamples = alac->setinfo_max_samples_per_frame;
886 int uncompressed_bytes;
888 uint8_t interlacing_shift;
889 uint8_t interlacing_leftweight;
891 /* 2^result = something to do with output waiting.
892 * perhaps matters if we read > 1 frame in a pass?
894 readbits(alac, 4);
896 readbits(alac, 12); /* unknown, skip 12 bits */
898 hassize = readbits(alac, 1); /* the output sample size is stored soon */
900 /* the number of bytes in the (compressed) stream that are not compressed */
901 uncompressed_bytes = readbits(alac, 2);
903 isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
905 if (hassize)
907 /* now read the number of samples,
908 * as a 32bit integer */
909 outputsamples = readbits(alac, 32);
912 readsamplesize = infosamplesize - (uncompressed_bytes * 8) + 1;
914 yield();
915 if (!isnotcompressed)
916 { /* compressed */
917 int predictor_coef_num_a;
918 int prediction_type_a;
919 int prediction_quantitization_a;
920 int ricemodifier_a;
922 int predictor_coef_num_b;
923 int prediction_type_b;
924 int prediction_quantitization_b;
925 int ricemodifier_b;
927 int i;
929 interlacing_shift = readbits(alac, 8);
930 interlacing_leftweight = readbits(alac, 8);
932 /******** channel 1 ***********/
933 prediction_type_a = readbits(alac, 4);
934 prediction_quantitization_a = readbits(alac, 4);
936 ricemodifier_a = readbits(alac, 3);
937 predictor_coef_num_a = readbits(alac, 5);
939 /* read the predictor table */
940 for (i = 0; i < predictor_coef_num_a; i++)
942 predictor_coef_table_a[i] = (int16_t)readbits(alac, 16);
945 /******** channel 2 *********/
946 prediction_type_b = readbits(alac, 4);
947 prediction_quantitization_b = readbits(alac, 4);
949 ricemodifier_b = readbits(alac, 3);
950 predictor_coef_num_b = readbits(alac, 5);
952 /* read the predictor table */
953 for (i = 0; i < predictor_coef_num_b; i++)
955 predictor_coef_table_b[i] = (int16_t)readbits(alac, 16);
958 /*********************/
959 if (uncompressed_bytes)
960 { /* see mono case */
961 int i;
962 for (i = 0; i < outputsamples; i++)
964 outputbuffer[0][i] = readbits(alac, uncompressed_bytes * 8);
965 outputbuffer[1][i] = readbits(alac, uncompressed_bytes * 8);
969 yield();
970 /* channel 1 */
971 entropy_rice_decode(alac,
972 outputbuffer[0],
973 outputsamples,
974 readsamplesize,
975 alac->setinfo_rice_initialhistory,
976 alac->setinfo_rice_kmodifier,
977 ricemodifier_a * alac->setinfo_rice_historymult / 4,
978 (1 << alac->setinfo_rice_kmodifier) - 1);
980 yield();
981 if (prediction_type_a == 0)
982 { /* adaptive fir */
983 predictor_decompress_fir_adapt(outputbuffer[0],
984 outputbuffer[0],
985 outputsamples,
986 readsamplesize,
987 predictor_coef_table_a,
988 predictor_coef_num_a,
989 prediction_quantitization_a);
991 else
992 { /* see mono case */
993 //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_a);
996 yield();
998 /* channel 2 */
999 entropy_rice_decode(alac,
1000 outputbuffer[1],
1001 outputsamples,
1002 readsamplesize,
1003 alac->setinfo_rice_initialhistory,
1004 alac->setinfo_rice_kmodifier,
1005 ricemodifier_b * alac->setinfo_rice_historymult / 4,
1006 (1 << alac->setinfo_rice_kmodifier) - 1);
1008 yield();
1009 if (prediction_type_b == 0)
1010 { /* adaptive fir */
1011 predictor_decompress_fir_adapt(outputbuffer[1],
1012 outputbuffer[1],
1013 outputsamples,
1014 readsamplesize,
1015 predictor_coef_table_b,
1016 predictor_coef_num_b,
1017 prediction_quantitization_b);
1019 else
1021 //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_b);
1024 else
1025 { /* not compressed, easy case */
1026 if (infosamplesize <= 16)
1028 int i;
1029 for (i = 0; i < outputsamples; i++)
1031 int32_t audiobits_a, audiobits_b;
1033 audiobits_a = readbits(alac, infosamplesize);
1034 audiobits_b = readbits(alac, infosamplesize);
1036 audiobits_a = SIGN_EXTENDED32(audiobits_a, infosamplesize);
1037 audiobits_b = SIGN_EXTENDED32(audiobits_b, infosamplesize);
1039 outputbuffer[0][i] = audiobits_a;
1040 outputbuffer[1][i] = audiobits_b;
1043 else
1045 int i;
1046 for (i = 0; i < outputsamples; i++)
1048 int32_t audiobits_a, audiobits_b;
1050 audiobits_a = readbits(alac, 16);
1051 audiobits_a = audiobits_a << (infosamplesize - 16);
1052 audiobits_a |= readbits(alac, infosamplesize - 16);
1053 audiobits_a = SIGNEXTEND24(audiobits_a);
1055 audiobits_b = readbits(alac, 16);
1056 audiobits_b = audiobits_b << (infosamplesize - 16);
1057 audiobits_b |= readbits(alac, infosamplesize - 16);
1058 audiobits_b = SIGNEXTEND24(audiobits_b);
1060 outputbuffer[0][i] = audiobits_a;
1061 outputbuffer[1][i] = audiobits_b;
1064 uncompressed_bytes = 0; // always 0 for uncompressed
1065 interlacing_shift = 0;
1066 interlacing_leftweight = 0;
1069 yield();
1071 switch(infosamplesize)
1073 case 16:
1075 deinterlace_16(outputbuffer[0],
1076 outputbuffer[1],
1077 outputsamples,
1078 interlacing_shift,
1079 interlacing_leftweight);
1080 break;
1082 case 24:
1084 deinterlace_24(outputbuffer[0],
1085 outputbuffer[1],
1086 uncompressed_bytes,
1087 outputbuffer[0],
1088 outputbuffer[1],
1089 outputsamples,
1090 interlacing_shift,
1091 interlacing_leftweight);
1092 break;
1094 case 20:
1095 case 32:
1096 //fprintf(stderr, "FIXME: unimplemented sample size %i\n", infosamplesize);
1097 break;
1098 default:
1099 break;
1101 return outputsamples;
1104 int alac_decode_frame(alac_file *alac,
1105 unsigned char *inbuffer,
1106 int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE],
1107 void (*yield)(void))
1109 int channels;
1110 int outputsamples;
1112 /* setup the stream */
1113 alac->input_buffer = inbuffer;
1114 alac->input_buffer_bitaccumulator = 0;
1116 channels = readbits(alac, 3);
1118 /* TODO: The mono and stereo functions should be combined. */
1119 switch(channels)
1121 case 0: /* 1 channel */
1122 outputsamples=decode_frame_mono(alac,outputbuffer,yield);
1123 break;
1124 case 1: /* 2 channels */
1125 outputsamples=decode_frame_stereo(alac,outputbuffer,yield);
1126 break;
1127 default: /* Unsupported */
1128 return -1;
1130 return outputsamples;
1133 void create_alac(int samplesize, int numchannels, alac_file* alac)
1135 alac->samplesize = samplesize;
1136 alac->numchannels = numchannels;
1137 alac->bytespersample = (samplesize / 8) * numchannels;