2 * ALAC (Apple Lossless Audio Codec) decoder
3 * Copyright (c) 2005 David Hammerton
6 * This is the actual decoder.
8 * http://crazney.net/programs/itunes/alac.html
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.
32 static const int host_bigendian
= 0;
38 #include "stdint_win.h"
47 v = (((v)&0x000000FF) << 0x18) | (((v)&0x0000FF00) << 0x08) | (((v)&0x00FF0000) >> 0x08) | \
48 (((v)&0xFF000000) >> 0x18); \
53 v = (((v)&0x00FF) << 0x08) | (((v)&0xFF00) >> 0x08); \
59 #define SignExtend24(val) (se_struct_24.x = val)
61 void alac_free(alac_file
*alac
) {
62 if (alac
->predicterror_buffer_a
)
63 free(alac
->predicterror_buffer_a
);
64 if (alac
->predicterror_buffer_b
)
65 free(alac
->predicterror_buffer_b
);
67 if (alac
->outputsamples_buffer_a
)
68 free(alac
->outputsamples_buffer_a
);
69 if (alac
->outputsamples_buffer_b
)
70 free(alac
->outputsamples_buffer_b
);
72 if (alac
->uncompressed_bytes_buffer_a
)
73 free(alac
->uncompressed_bytes_buffer_a
);
74 if (alac
->uncompressed_bytes_buffer_b
)
75 free(alac
->uncompressed_bytes_buffer_b
);
80 void alac_allocate_buffers(alac_file
*alac
) {
81 alac
->predicterror_buffer_a
= malloc(alac
->setinfo_max_samples_per_frame
* 4);
82 alac
->predicterror_buffer_b
= malloc(alac
->setinfo_max_samples_per_frame
* 4);
84 alac
->outputsamples_buffer_a
= malloc(alac
->setinfo_max_samples_per_frame
* 4);
85 alac
->outputsamples_buffer_b
= malloc(alac
->setinfo_max_samples_per_frame
* 4);
87 alac
->uncompressed_bytes_buffer_a
= malloc(alac
->setinfo_max_samples_per_frame
* 4);
88 alac
->uncompressed_bytes_buffer_b
= malloc(alac
->setinfo_max_samples_per_frame
* 4);
91 void alac_set_info(alac_file
*alac
, char *inputbuffer
) {
92 char *ptr
= inputbuffer
;
101 alac
->setinfo_max_samples_per_frame
= *(uint32_t *)ptr
; /* buffer size / 2 ? */
103 _Swap32(alac
->setinfo_max_samples_per_frame
);
105 alac
->setinfo_7a
= *(uint8_t *)ptr
;
107 alac
->setinfo_sample_size
= *(uint8_t *)ptr
;
109 alac
->setinfo_rice_historymult
= *(uint8_t *)ptr
;
111 alac
->setinfo_rice_initialhistory
= *(uint8_t *)ptr
;
113 alac
->setinfo_rice_kmodifier
= *(uint8_t *)ptr
;
115 alac
->setinfo_7f
= *(uint8_t *)ptr
;
117 alac
->setinfo_80
= *(uint16_t *)ptr
;
119 _Swap16(alac
->setinfo_80
);
121 alac
->setinfo_82
= *(uint32_t *)ptr
;
123 _Swap32(alac
->setinfo_82
);
125 alac
->setinfo_86
= *(uint32_t *)ptr
;
127 _Swap32(alac
->setinfo_86
);
129 alac
->setinfo_8a_rate
= *(uint32_t *)ptr
;
131 _Swap32(alac
->setinfo_8a_rate
);
133 alac_allocate_buffers(alac
);
138 /* supports reading 1 to 16 bits, in big endian format */
139 static uint32_t readbits_16(alac_file
*alac
, int bits
) {
143 result
= (alac
->input_buffer
[0] << 16) | (alac
->input_buffer
[1] << 8) | (alac
->input_buffer
[2]);
145 /* shift left by the number of bits we've already read,
146 * so that the top 'n' bits of the 24 bits we read will
147 * be the return bits */
148 result
= result
<< alac
->input_buffer_bitaccumulator
;
150 result
= result
& 0x00ffffff;
152 /* and then only want the top 'n' bits from that, where
154 result
= result
>> (24 - bits
);
156 new_accumulator
= (alac
->input_buffer_bitaccumulator
+ bits
);
158 /* increase the buffer pointer if we've read over n bytes. */
159 alac
->input_buffer
+= (new_accumulator
>> 3);
161 /* and the remainder goes back into the bit accumulator */
162 alac
->input_buffer_bitaccumulator
= (new_accumulator
& 7);
167 /* supports reading 1 to 32 bits, in big endian format */
168 static uint32_t readbits(alac_file
*alac
, int bits
) {
173 result
= readbits_16(alac
, 16) << bits
;
176 result
|= readbits_16(alac
, bits
);
181 /* reads a single bit */
182 static int readbit(alac_file
*alac
) {
186 result
= alac
->input_buffer
[0];
188 result
= result
<< alac
->input_buffer_bitaccumulator
;
190 result
= result
>> 7 & 1;
192 new_accumulator
= (alac
->input_buffer_bitaccumulator
+ 1);
194 alac
->input_buffer
+= (new_accumulator
/ 8);
196 alac
->input_buffer_bitaccumulator
= (new_accumulator
% 8);
201 static void unreadbits(alac_file
*alac
, int bits
) {
202 int new_accumulator
= (alac
->input_buffer_bitaccumulator
- bits
);
204 alac
->input_buffer
+= (new_accumulator
>> 3);
206 alac
->input_buffer_bitaccumulator
= (new_accumulator
& 7);
207 if (alac
->input_buffer_bitaccumulator
< 0)
208 alac
->input_buffer_bitaccumulator
*= -1;
211 /* various implementations of count_leading_zero:
212 * the first one is the original one, the simplest and most
213 * obvious for what it's doing. never use this.
214 * then there are the asm ones. fill in as necessary
215 * and finally an unrolled and optimised c version
219 /* hideously inefficient. could use a bitmask search,
220 * alternatively bsr on x86,
222 static int count_leading_zeros(int32_t input
)
225 while (!(0x80000000 & input
) && i
< 32)
232 #elif defined(__GNUC__)
233 /* for some reason the unrolled version (below) is
234 * actually faster than this. yay intel!
236 static int count_leading_zeros(int input
) { return __builtin_clz(input
); }
237 #elif defined(_MSC_VER) && defined(_M_IX86)
238 static int count_leading_zeros(int input
) {
253 #warning using generic count leading zeroes. You may wish to write one for your CPU / compiler
254 static int count_leading_zeros(int input
) {
258 curbyte
= input
>> 24;
263 curbyte
= input
>> 16;
268 curbyte
= input
>> 8;
281 if (!(curbyte
& 0xf0)) {
295 /* shouldn't get here: */
300 #define RICE_THRESHOLD 8 // maximum number of bits for a rice prefix.
302 static int32_t entropy_decode_value(alac_file
*alac
, int readSampleSize
, int k
,
303 int rice_kmodifier_mask
) {
304 int32_t x
= 0; // decoded value
306 // read x, number of 1s before 0 represent the rice value.
307 while (x
<= RICE_THRESHOLD
&& readbit(alac
)) {
311 if (x
> RICE_THRESHOLD
) {
312 // read the number from the bit stream (raw value)
315 value
= readbits(alac
, readSampleSize
);
318 value
&= (((uint32_t)0xffffffff) >> (32 - readSampleSize
));
323 int extraBits
= readbits(alac
, k
);
326 x
*= (((1 << k
) - 1) & rice_kmodifier_mask
);
338 static void entropy_rice_decode(alac_file
*alac
, int32_t *outputBuffer
, int outputSize
,
339 int readSampleSize
, int rice_initialhistory
, int rice_kmodifier
,
340 int rice_historymult
, int rice_kmodifier_mask
) {
342 int history
= rice_initialhistory
;
343 int signModifier
= 0;
345 for (outputCount
= 0; outputCount
< outputSize
; outputCount
++) {
346 int32_t decodedValue
;
350 k
= 31 - rice_kmodifier
- count_leading_zeros((history
>> 9) + 3);
357 // note: don't use rice_kmodifier_mask here (set mask to 0xFFFFFFFF)
358 decodedValue
= entropy_decode_value(alac
, readSampleSize
, k
, 0xFFFFFFFF);
360 decodedValue
+= signModifier
;
361 finalValue
= (decodedValue
+ 1) / 2; // inc by 1 and shift out sign bit
362 if (decodedValue
& 1) // the sign is stored in the low bit
365 outputBuffer
[outputCount
] = finalValue
;
370 history
+= (decodedValue
* rice_historymult
) - ((history
* rice_historymult
) >> 9);
372 if (decodedValue
> 0xFFFF)
375 // special case, for compressed blocks of 0
376 if ((history
< 128) && (outputCount
+ 1 < outputSize
)) {
381 k
= count_leading_zeros(history
) + ((history
+ 16) / 64) - 24;
383 // note: blockSize is always 16bit
384 blockSize
= entropy_decode_value(alac
, 16, k
, rice_kmodifier_mask
);
388 memset(&outputBuffer
[outputCount
+ 1], 0, blockSize
* sizeof(*outputBuffer
));
389 outputCount
+= blockSize
;
392 if (blockSize
> 0xFFFF)
400 #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
402 #define SIGN_ONLY(v) ((v < 0) ? (-1) : ((v > 0) ? (1) : (0)))
404 static void predictor_decompress_fir_adapt(int32_t *error_buffer
, int32_t *buffer_out
,
405 int output_size
, int readsamplesize
,
406 int16_t *predictor_coef_table
, int predictor_coef_num
,
407 int predictor_quantitization
) {
410 /* first sample always copies */
411 *buffer_out
= *error_buffer
;
413 if (!predictor_coef_num
) {
414 if (output_size
<= 1)
416 memcpy(buffer_out
+ 1, error_buffer
+ 1, (output_size
- 1) * 4);
420 if (predictor_coef_num
== 0x1f) /* 11111 - max value of predictor_coef_num */
421 { /* second-best case scenario for fir decompression,
422 * error describes a small difference from the previous sample only
424 if (output_size
<= 1)
426 for (i
= 0; i
< output_size
- 1; i
++) {
430 prev_value
= buffer_out
[i
];
431 error_value
= error_buffer
[i
+ 1];
432 buffer_out
[i
+ 1] = SIGN_EXTENDED32((prev_value
+ error_value
), readsamplesize
);
437 /* read warm-up samples */
438 if (predictor_coef_num
> 0) {
440 for (i
= 0; i
< predictor_coef_num
; i
++) {
443 val
= buffer_out
[i
] + error_buffer
[i
+ 1];
445 val
= SIGN_EXTENDED32(val
, readsamplesize
);
447 buffer_out
[i
+ 1] = val
;
452 /* 4 and 8 are very common cases (the only ones i've seen). these
453 * should be unrolled and optimised
455 if (predictor_coef_num
== 4)
457 /* FIXME: optimised general case */
461 if (predictor_coef_table
== 8)
463 /* FIXME: optimised general case */
469 if (predictor_coef_num
> 0) {
470 for (i
= predictor_coef_num
+ 1; i
< output_size
; i
++) {
474 int error_val
= error_buffer
[i
];
476 for (j
= 0; j
< predictor_coef_num
; j
++) {
477 sum
+= (buffer_out
[predictor_coef_num
- j
] - buffer_out
[0]) * predictor_coef_table
[j
];
480 outval
= (1 << (predictor_quantitization
- 1)) + sum
;
481 outval
= outval
>> predictor_quantitization
;
482 outval
= outval
+ buffer_out
[0] + error_val
;
483 outval
= SIGN_EXTENDED32(outval
, readsamplesize
);
485 buffer_out
[predictor_coef_num
+ 1] = outval
;
488 int predictor_num
= predictor_coef_num
- 1;
490 while (predictor_num
>= 0 && error_val
> 0) {
491 int val
= buffer_out
[0] - buffer_out
[predictor_coef_num
- predictor_num
];
492 int sign
= SIGN_ONLY(val
);
494 predictor_coef_table
[predictor_num
] -= sign
;
496 val
*= sign
; /* absolute value */
498 error_val
-= ((val
>> predictor_quantitization
) * (predictor_coef_num
- predictor_num
));
502 } else if (error_val
< 0) {
503 int predictor_num
= predictor_coef_num
- 1;
505 while (predictor_num
>= 0 && error_val
< 0) {
506 int val
= buffer_out
[0] - buffer_out
[predictor_coef_num
- predictor_num
];
507 int sign
= -SIGN_ONLY(val
);
509 predictor_coef_table
[predictor_num
] -= sign
;
511 val
*= sign
; /* neg value */
513 error_val
-= ((val
>> predictor_quantitization
) * (predictor_coef_num
- predictor_num
));
524 static void deinterlace_16(int32_t *buffer_a
, int32_t *buffer_b
, int16_t *buffer_out
,
525 int numchannels
, int numsamples
, uint8_t interlacing_shift
,
526 uint8_t interlacing_leftweight
) {
531 /* weighted interlacing */
532 if (interlacing_leftweight
) {
533 for (i
= 0; i
< numsamples
; i
++) {
534 int32_t difference
, midright
;
538 midright
= buffer_a
[i
];
539 difference
= buffer_b
[i
];
541 right
= midright
- ((difference
* interlacing_leftweight
) >> interlacing_shift
);
542 left
= right
+ difference
;
544 /* output is always little endian */
545 if (host_bigendian
) {
550 buffer_out
[i
* numchannels
] = left
;
551 buffer_out
[i
* numchannels
+ 1] = right
;
557 /* otherwise basic interlacing took place */
558 for (i
= 0; i
< numsamples
; i
++) {
564 /* output is always little endian */
565 if (host_bigendian
) {
570 buffer_out
[i
* numchannels
] = left
;
571 buffer_out
[i
* numchannels
+ 1] = right
;
575 static void deinterlace_24(int32_t *buffer_a
, int32_t *buffer_b
, int uncompressed_bytes
,
576 int32_t *uncompressed_bytes_buffer_a
,
577 int32_t *uncompressed_bytes_buffer_b
, void *buffer_out
, int numchannels
,
578 int numsamples
, uint8_t interlacing_shift
,
579 uint8_t interlacing_leftweight
) {
584 /* weighted interlacing */
585 if (interlacing_leftweight
) {
586 for (i
= 0; i
< numsamples
; i
++) {
587 int32_t difference
, midright
;
591 midright
= buffer_a
[i
];
592 difference
= buffer_b
[i
];
594 right
= midright
- ((difference
* interlacing_leftweight
) >> interlacing_shift
);
595 left
= right
+ difference
;
597 if (uncompressed_bytes
) {
598 uint32_t mask
= ~(0xFFFFFFFF << (uncompressed_bytes
* 8));
599 left
<<= (uncompressed_bytes
* 8);
600 right
<<= (uncompressed_bytes
* 8);
602 left
|= uncompressed_bytes_buffer_a
[i
] & mask
;
603 right
|= uncompressed_bytes_buffer_b
[i
] & mask
;
606 ((uint8_t *)buffer_out
)[i
* numchannels
* 3] = (left
)&0xFF;
607 ((uint8_t *)buffer_out
)[i
* numchannels
* 3 + 1] = (left
>> 8) & 0xFF;
608 ((uint8_t *)buffer_out
)[i
* numchannels
* 3 + 2] = (left
>> 16) & 0xFF;
610 ((uint8_t *)buffer_out
)[i
* numchannels
* 3 + 3] = (right
)&0xFF;
611 ((uint8_t *)buffer_out
)[i
* numchannels
* 3 + 4] = (right
>> 8) & 0xFF;
612 ((uint8_t *)buffer_out
)[i
* numchannels
* 3 + 5] = (right
>> 16) & 0xFF;
618 /* otherwise basic interlacing took place */
619 for (i
= 0; i
< numsamples
; i
++) {
625 if (uncompressed_bytes
) {
626 uint32_t mask
= ~(0xFFFFFFFF << (uncompressed_bytes
* 8));
627 left
<<= (uncompressed_bytes
* 8);
628 right
<<= (uncompressed_bytes
* 8);
630 left
|= uncompressed_bytes_buffer_a
[i
] & mask
;
631 right
|= uncompressed_bytes_buffer_b
[i
] & mask
;
634 ((uint8_t *)buffer_out
)[i
* numchannels
* 3] = (left
)&0xFF;
635 ((uint8_t *)buffer_out
)[i
* numchannels
* 3 + 1] = (left
>> 8) & 0xFF;
636 ((uint8_t *)buffer_out
)[i
* numchannels
* 3 + 2] = (left
>> 16) & 0xFF;
638 ((uint8_t *)buffer_out
)[i
* numchannels
* 3 + 3] = (right
)&0xFF;
639 ((uint8_t *)buffer_out
)[i
* numchannels
* 3 + 4] = (right
>> 8) & 0xFF;
640 ((uint8_t *)buffer_out
)[i
* numchannels
* 3 + 5] = (right
>> 16) & 0xFF;
644 void alac_decode_frame(alac_file
*alac
, unsigned char *inbuffer
, void *outbuffer
, int *outputsize
) {
645 int outbuffer_allocation_size
= *outputsize
; // initial value
647 int32_t outputsamples
= alac
->setinfo_max_samples_per_frame
;
649 /* setup the stream */
650 alac
->input_buffer
= inbuffer
;
651 alac
->input_buffer_bitaccumulator
= 0;
653 channels
= readbits(alac
, 3);
655 *outputsize
= outputsamples
* alac
->bytespersample
;
656 if (*outputsize
> outbuffer_allocation_size
) {
657 fprintf(stderr
, "FIXME: Not enough space if the output buffer for audio frame - E1.\n");
663 case 0: /* 1 channel */
669 int uncompressed_bytes
;
672 /* 2^result = something to do with output waiting.
673 * perhaps matters if we read > 1 frame in a pass?
677 readbits(alac
, 12); /* unknown, skip 12 bits */
679 hassize
= readbits(alac
, 1); /* the output sample size is stored soon */
682 readbits(alac
, 2); /* number of bytes in the (compressed) stream that are not compressed */
684 isnotcompressed
= readbits(alac
, 1); /* whether the frame is compressed */
687 /* now read the number of samples,
688 * as a 32bit integer */
689 outputsamples
= readbits(alac
, 32);
690 *outputsize
= outputsamples
* alac
->bytespersample
;
691 if (*outputsize
> outbuffer_allocation_size
) {
692 fprintf(stderr
, "FIXME: Not enough space if the output buffer for audio frame - E2.\n");
698 readsamplesize
= alac
->setinfo_sample_size
- (uncompressed_bytes
* 8);
700 if (!isnotcompressed
) { /* so it is compressed */
701 int16_t predictor_coef_table
[32];
702 int predictor_coef_num
;
704 int prediction_quantitization
;
707 /* skip 16 bits, not sure what they are. seem to be used in
708 * two channel case */
712 prediction_type
= readbits(alac
, 4);
713 prediction_quantitization
= readbits(alac
, 4);
715 ricemodifier
= readbits(alac
, 3);
716 predictor_coef_num
= readbits(alac
, 5);
718 /* read the predictor table */
719 for (i
= 0; i
< predictor_coef_num
; i
++) {
720 predictor_coef_table
[i
] = (int16_t)readbits(alac
, 16);
723 if (uncompressed_bytes
) {
725 for (i
= 0; i
< outputsamples
; i
++) {
726 alac
->uncompressed_bytes_buffer_a
[i
] = readbits(alac
, uncompressed_bytes
* 8);
730 entropy_rice_decode(alac
, alac
->predicterror_buffer_a
, outputsamples
, readsamplesize
,
731 alac
->setinfo_rice_initialhistory
, alac
->setinfo_rice_kmodifier
,
732 ricemodifier
* alac
->setinfo_rice_historymult
/ 4,
733 (1 << alac
->setinfo_rice_kmodifier
) - 1);
735 if (prediction_type
== 0) { /* adaptive fir */
736 predictor_decompress_fir_adapt(alac
->predicterror_buffer_a
, alac
->outputsamples_buffer_a
,
737 outputsamples
, readsamplesize
, predictor_coef_table
,
738 predictor_coef_num
, prediction_quantitization
);
740 fprintf(stderr
, "FIXME: unhandled prediction type for compressed case: %i\n",
742 /* i think the only other prediction type (or perhaps this is just a
743 * boolean?) runs adaptive fir twice.. like:
744 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
745 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
750 } else { /* not compressed, easy case */
751 if (alac
->setinfo_sample_size
<= 16) {
753 for (i
= 0; i
< outputsamples
; i
++) {
754 int32_t audiobits
= readbits(alac
, alac
->setinfo_sample_size
);
756 audiobits
= SIGN_EXTENDED32(audiobits
, alac
->setinfo_sample_size
);
758 alac
->outputsamples_buffer_a
[i
] = audiobits
;
762 for (i
= 0; i
< outputsamples
; i
++) {
765 audiobits
= readbits(alac
, 16);
766 /* special case of sign extension..
767 * as we'll be ORing the low 16bits into this */
768 audiobits
= audiobits
<< (alac
->setinfo_sample_size
- 16);
769 audiobits
|= readbits(alac
, alac
->setinfo_sample_size
- 16);
770 audiobits
= SignExtend24(audiobits
);
772 alac
->outputsamples_buffer_a
[i
] = audiobits
;
775 uncompressed_bytes
= 0; // always 0 for uncompressed
778 switch (alac
->setinfo_sample_size
) {
781 for (i
= 0; i
< outputsamples
; i
++) {
782 int16_t sample
= alac
->outputsamples_buffer_a
[i
];
785 ((int16_t *)outbuffer
)[i
* alac
->numchannels
] = sample
;
791 for (i
= 0; i
< outputsamples
; i
++) {
792 int32_t sample
= alac
->outputsamples_buffer_a
[i
];
794 if (uncompressed_bytes
) {
796 sample
= sample
<< (uncompressed_bytes
* 8);
797 mask
= ~(0xFFFFFFFF << (uncompressed_bytes
* 8));
798 sample
|= alac
->uncompressed_bytes_buffer_a
[i
] & mask
;
801 ((uint8_t *)outbuffer
)[i
* alac
->numchannels
* 3] = (sample
)&0xFF;
802 ((uint8_t *)outbuffer
)[i
* alac
->numchannels
* 3 + 1] = (sample
>> 8) & 0xFF;
803 ((uint8_t *)outbuffer
)[i
* alac
->numchannels
* 3 + 2] = (sample
>> 16) & 0xFF;
809 fprintf(stderr
, "FIXME: unimplemented sample size %i\n", alac
->setinfo_sample_size
);
816 case 1: /* 2 channels */
822 int uncompressed_bytes
;
824 uint8_t interlacing_shift
;
825 uint8_t interlacing_leftweight
;
827 /* 2^result = something to do with output waiting.
828 * perhaps matters if we read > 1 frame in a pass?
832 readbits(alac
, 12); /* unknown, skip 12 bits */
834 hassize
= readbits(alac
, 1); /* the output sample size is stored soon */
836 uncompressed_bytes
= readbits(
837 alac
, 2); /* the number of bytes in the (compressed) stream that are not compressed */
839 isnotcompressed
= readbits(alac
, 1); /* whether the frame is compressed */
842 /* now read the number of samples,
843 * as a 32bit integer */
844 outputsamples
= readbits(alac
, 32);
845 *outputsize
= outputsamples
* alac
->bytespersample
;
846 if (*outputsize
> outbuffer_allocation_size
) {
847 fprintf(stderr
, "FIXME: Not enough space if the output buffer for audio frame - E3.\n");
853 readsamplesize
= alac
->setinfo_sample_size
- (uncompressed_bytes
* 8) + 1;
855 if (!isnotcompressed
) { /* compressed */
856 int16_t predictor_coef_table_a
[32];
857 int predictor_coef_num_a
;
858 int prediction_type_a
;
859 int prediction_quantitization_a
;
862 int16_t predictor_coef_table_b
[32];
863 int predictor_coef_num_b
;
864 int prediction_type_b
;
865 int prediction_quantitization_b
;
870 interlacing_shift
= readbits(alac
, 8);
871 interlacing_leftweight
= readbits(alac
, 8);
873 /******** channel 1 ***********/
874 prediction_type_a
= readbits(alac
, 4);
875 prediction_quantitization_a
= readbits(alac
, 4);
877 ricemodifier_a
= readbits(alac
, 3);
878 predictor_coef_num_a
= readbits(alac
, 5);
880 /* read the predictor table */
881 for (i
= 0; i
< predictor_coef_num_a
; i
++) {
882 predictor_coef_table_a
[i
] = (int16_t)readbits(alac
, 16);
885 /******** channel 2 *********/
886 prediction_type_b
= readbits(alac
, 4);
887 prediction_quantitization_b
= readbits(alac
, 4);
889 ricemodifier_b
= readbits(alac
, 3);
890 predictor_coef_num_b
= readbits(alac
, 5);
892 /* read the predictor table */
893 for (i
= 0; i
< predictor_coef_num_b
; i
++) {
894 predictor_coef_table_b
[i
] = (int16_t)readbits(alac
, 16);
897 /*********************/
898 if (uncompressed_bytes
) { /* see mono case */
900 for (i
= 0; i
< outputsamples
; i
++) {
901 alac
->uncompressed_bytes_buffer_a
[i
] = readbits(alac
, uncompressed_bytes
* 8);
902 alac
->uncompressed_bytes_buffer_b
[i
] = readbits(alac
, uncompressed_bytes
* 8);
907 entropy_rice_decode(alac
, alac
->predicterror_buffer_a
, outputsamples
, readsamplesize
,
908 alac
->setinfo_rice_initialhistory
, alac
->setinfo_rice_kmodifier
,
909 ricemodifier_a
* alac
->setinfo_rice_historymult
/ 4,
910 (1 << alac
->setinfo_rice_kmodifier
) - 1);
912 if (prediction_type_a
== 0) { /* adaptive fir */
913 predictor_decompress_fir_adapt(alac
->predicterror_buffer_a
, alac
->outputsamples_buffer_a
,
914 outputsamples
, readsamplesize
, predictor_coef_table_a
,
915 predictor_coef_num_a
, prediction_quantitization_a
);
916 } else { /* see mono case */
917 fprintf(stderr
, "FIXME: unhandled prediction type on channel 1: %i\n", prediction_type_a
);
921 entropy_rice_decode(alac
, alac
->predicterror_buffer_b
, outputsamples
, readsamplesize
,
922 alac
->setinfo_rice_initialhistory
, alac
->setinfo_rice_kmodifier
,
923 ricemodifier_b
* alac
->setinfo_rice_historymult
/ 4,
924 (1 << alac
->setinfo_rice_kmodifier
) - 1);
926 if (prediction_type_b
== 0) { /* adaptive fir */
927 predictor_decompress_fir_adapt(alac
->predicterror_buffer_b
, alac
->outputsamples_buffer_b
,
928 outputsamples
, readsamplesize
, predictor_coef_table_b
,
929 predictor_coef_num_b
, prediction_quantitization_b
);
931 fprintf(stderr
, "FIXME: unhandled prediction type on channel 2: %i\n", prediction_type_b
);
933 } else { /* not compressed, easy case */
934 if (alac
->setinfo_sample_size
<= 16) {
936 for (i
= 0; i
< outputsamples
; i
++) {
937 int32_t audiobits_a
, audiobits_b
;
939 audiobits_a
= readbits(alac
, alac
->setinfo_sample_size
);
940 audiobits_b
= readbits(alac
, alac
->setinfo_sample_size
);
942 audiobits_a
= SIGN_EXTENDED32(audiobits_a
, alac
->setinfo_sample_size
);
943 audiobits_b
= SIGN_EXTENDED32(audiobits_b
, alac
->setinfo_sample_size
);
945 alac
->outputsamples_buffer_a
[i
] = audiobits_a
;
946 alac
->outputsamples_buffer_b
[i
] = audiobits_b
;
950 for (i
= 0; i
< outputsamples
; i
++) {
951 int32_t audiobits_a
, audiobits_b
;
953 audiobits_a
= readbits(alac
, 16);
954 audiobits_a
= audiobits_a
<< (alac
->setinfo_sample_size
- 16);
955 audiobits_a
|= readbits(alac
, alac
->setinfo_sample_size
- 16);
956 audiobits_a
= SignExtend24(audiobits_a
);
958 audiobits_b
= readbits(alac
, 16);
959 audiobits_b
= audiobits_b
<< (alac
->setinfo_sample_size
- 16);
960 audiobits_b
|= readbits(alac
, alac
->setinfo_sample_size
- 16);
961 audiobits_b
= SignExtend24(audiobits_b
);
963 alac
->outputsamples_buffer_a
[i
] = audiobits_a
;
964 alac
->outputsamples_buffer_b
[i
] = audiobits_b
;
967 uncompressed_bytes
= 0; // always 0 for uncompressed
968 interlacing_shift
= 0;
969 interlacing_leftweight
= 0;
972 switch (alac
->setinfo_sample_size
) {
974 deinterlace_16(alac
->outputsamples_buffer_a
, alac
->outputsamples_buffer_b
,
975 (int16_t *)outbuffer
, alac
->numchannels
, outputsamples
, interlacing_shift
,
976 interlacing_leftweight
);
980 deinterlace_24(alac
->outputsamples_buffer_a
, alac
->outputsamples_buffer_b
, uncompressed_bytes
,
981 alac
->uncompressed_bytes_buffer_a
, alac
->uncompressed_bytes_buffer_b
,
982 (int16_t *)outbuffer
, alac
->numchannels
, outputsamples
, interlacing_shift
,
983 interlacing_leftweight
);
988 fprintf(stderr
, "FIXME: unimplemented sample size %i\n", alac
->setinfo_sample_size
);
999 alac_file
*alac_create(int samplesize
, int numchannels
) {
1000 alac_file
*newfile
= malloc(sizeof(alac_file
));
1002 memset(newfile
, 0, sizeof(alac_file
));
1003 newfile
->samplesize
= samplesize
;
1004 newfile
->numchannels
= numchannels
;
1005 newfile
->bytespersample
= (samplesize
/ 8) * numchannels
;
1007 fprintf(stderr
, "FIXME: can not allocate memory for a new file in alac_cxreate.");