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.
33 static const int host_bigendian
= 1;
35 static const int host_bigendian
= 0;
42 #include "stdint_win.h"
49 #define _Swap32(v) do { \
50 v = (((v) & 0x000000FF) << 0x18) | \
51 (((v) & 0x0000FF00) << 0x08) | \
52 (((v) & 0x00FF0000) >> 0x08) | \
53 (((v) & 0xFF000000) >> 0x18); } while(0)
55 #define _Swap16(v) do { \
56 v = (((v) & 0x00FF) << 0x08) | \
57 (((v) & 0xFF00) >> 0x08); } while (0)
59 struct {signed int x
:24;} se_struct_24
;
60 #define SignExtend24(val) (se_struct_24.x = val)
64 unsigned char *input_buffer
;
65 int input_buffer_bitaccumulator
; /* used so we can do arbitary
74 int32_t *predicterror_buffer_a
;
75 int32_t *predicterror_buffer_b
;
77 int32_t *outputsamples_buffer_a
;
78 int32_t *outputsamples_buffer_b
;
80 int32_t *uncompressed_bytes_buffer_a
;
81 int32_t *uncompressed_bytes_buffer_b
;
85 /* stuff from setinfo */
86 uint32_t setinfo_max_samples_per_frame
; /* 0x1000 = 4096 */ /* max samples per frame? */
87 uint8_t setinfo_7a
; /* 0x00 */
88 uint8_t setinfo_sample_size
; /* 0x10 */
89 uint8_t setinfo_rice_historymult
; /* 0x28 */
90 uint8_t setinfo_rice_initialhistory
; /* 0x0a */
91 uint8_t setinfo_rice_kmodifier
; /* 0x0e */
92 uint8_t setinfo_7f
; /* 0x02 */
93 uint16_t setinfo_80
; /* 0x00ff */
94 uint32_t setinfo_82
; /* 0x000020e7 */ /* max sample size?? */
95 uint32_t setinfo_86
; /* 0x00069fe4 */ /* bit rate (avarge)?? */
96 uint32_t setinfo_8a_rate
; /* 0x0000ac44 */
97 /* end setinfo stuff */
102 static void allocate_buffers(alac_file
*alac
)
104 alac
->predicterror_buffer_a
= malloc(alac
->setinfo_max_samples_per_frame
* 4);
105 alac
->predicterror_buffer_b
= malloc(alac
->setinfo_max_samples_per_frame
* 4);
107 alac
->outputsamples_buffer_a
= malloc(alac
->setinfo_max_samples_per_frame
* 4);
108 alac
->outputsamples_buffer_b
= malloc(alac
->setinfo_max_samples_per_frame
* 4);
110 alac
->uncompressed_bytes_buffer_a
= malloc(alac
->setinfo_max_samples_per_frame
* 4);
111 alac
->uncompressed_bytes_buffer_b
= malloc(alac
->setinfo_max_samples_per_frame
* 4);
114 void alac_set_info(alac_file
*alac
, char *inputbuffer
)
116 char *ptr
= inputbuffer
;
125 alac
->setinfo_max_samples_per_frame
= *(uint32_t*)ptr
; /* buffer size / 2 ? */
127 _Swap32(alac
->setinfo_max_samples_per_frame
);
129 alac
->setinfo_7a
= *(uint8_t*)ptr
;
131 alac
->setinfo_sample_size
= *(uint8_t*)ptr
;
133 alac
->setinfo_rice_historymult
= *(uint8_t*)ptr
;
135 alac
->setinfo_rice_initialhistory
= *(uint8_t*)ptr
;
137 alac
->setinfo_rice_kmodifier
= *(uint8_t*)ptr
;
139 alac
->setinfo_7f
= *(uint8_t*)ptr
;
141 alac
->setinfo_80
= *(uint16_t*)ptr
;
143 _Swap16(alac
->setinfo_80
);
145 alac
->setinfo_82
= *(uint32_t*)ptr
;
147 _Swap32(alac
->setinfo_82
);
149 alac
->setinfo_86
= *(uint32_t*)ptr
;
151 _Swap32(alac
->setinfo_86
);
153 alac
->setinfo_8a_rate
= *(uint32_t*)ptr
;
155 _Swap32(alac
->setinfo_8a_rate
);
158 allocate_buffers(alac
);
164 /* supports reading 1 to 16 bits, in big endian format */
165 static uint32_t readbits_16(alac_file
*alac
, int bits
)
170 result
= (alac
->input_buffer
[0] << 16) |
171 (alac
->input_buffer
[1] << 8) |
172 (alac
->input_buffer
[2]);
174 /* shift left by the number of bits we've already read,
175 * so that the top 'n' bits of the 24 bits we read will
176 * be the return bits */
177 result
= result
<< alac
->input_buffer_bitaccumulator
;
179 result
= result
& 0x00ffffff;
181 /* and then only want the top 'n' bits from that, where
183 result
= result
>> (24 - bits
);
185 new_accumulator
= (alac
->input_buffer_bitaccumulator
+ bits
);
187 /* increase the buffer pointer if we've read over n bytes. */
188 alac
->input_buffer
+= (new_accumulator
>> 3);
190 /* and the remainder goes back into the bit accumulator */
191 alac
->input_buffer_bitaccumulator
= (new_accumulator
& 7);
196 /* supports reading 1 to 32 bits, in big endian format */
197 static uint32_t readbits(alac_file
*alac
, int bits
)
204 result
= readbits_16(alac
, 16) << bits
;
207 result
|= readbits_16(alac
, bits
);
212 /* reads a single bit */
213 static int readbit(alac_file
*alac
)
218 result
= alac
->input_buffer
[0];
220 result
= result
<< alac
->input_buffer_bitaccumulator
;
222 result
= result
>> 7 & 1;
224 new_accumulator
= (alac
->input_buffer_bitaccumulator
+ 1);
226 alac
->input_buffer
+= (new_accumulator
/ 8);
228 alac
->input_buffer_bitaccumulator
= (new_accumulator
% 8);
233 static void unreadbits(alac_file
*alac
, int bits
)
235 int new_accumulator
= (alac
->input_buffer_bitaccumulator
- bits
);
237 alac
->input_buffer
+= (new_accumulator
>> 3);
239 alac
->input_buffer_bitaccumulator
= (new_accumulator
& 7);
240 if (alac
->input_buffer_bitaccumulator
< 0)
241 alac
->input_buffer_bitaccumulator
*= -1;
244 /* various implementations of count_leading_zero:
245 * the first one is the original one, the simplest and most
246 * obvious for what it's doing. never use this.
247 * then there are the asm ones. fill in as necessary
248 * and finally an unrolled and optimised c version
252 /* hideously inefficient. could use a bitmask search,
253 * alternatively bsr on x86,
255 static int count_leading_zeros(int32_t input
)
258 while (!(0x80000000 & input
) && i
< 32)
265 #elif defined(__GNUC__) && (defined(_X86) || defined(__i386) || defined(i386))
266 /* for some reason the unrolled version (below) is
267 * actually faster than this. yay intel!
269 static int count_leading_zeros(int input
)
272 if (!input
) return 32;
276 return (0x1f - output
);
278 #elif defined(__GNUC__)
279 static int count_leading_zeros(int input
)
281 return __builtin_clz(input
);
283 #elif defined(_MSC_VER) && defined(_M_IX86)
284 static int count_leading_zeros(int input
)
287 if (!input
) return 32;
299 #warning using generic count leading zeroes. You may wish to write one for your CPU / compiler
300 static int count_leading_zeros(int input
)
305 curbyte
= input
>> 24;
306 if (curbyte
) goto found
;
309 curbyte
= input
>> 16;
310 if (curbyte
& 0xff) goto found
;
313 curbyte
= input
>> 8;
314 if (curbyte
& 0xff) goto found
;
318 if (curbyte
& 0xff) goto found
;
324 if (!(curbyte
& 0xf0))
340 /* shouldn't get here: */
345 #define RICE_THRESHOLD 8 // maximum number of bits for a rice prefix.
347 int32_t entropy_decode_value(alac_file
* alac
,
350 int rice_kmodifier_mask
)
352 int32_t x
= 0; // decoded value
354 // read x, number of 1s before 0 represent the rice value.
355 while (x
<= RICE_THRESHOLD
&& readbit(alac
))
360 if (x
> RICE_THRESHOLD
)
362 // read the number from the bit stream (raw value)
365 value
= readbits(alac
, readSampleSize
);
368 value
&= (((uint32_t)0xffffffff) >> (32 - readSampleSize
));
376 int extraBits
= readbits(alac
, k
);
379 x
*= (((1 << k
) - 1) & rice_kmodifier_mask
);
391 void entropy_rice_decode(alac_file
* alac
,
392 int32_t* outputBuffer
,
395 int rice_initialhistory
,
397 int rice_historymult
,
398 int rice_kmodifier_mask
)
401 int history
= rice_initialhistory
;
402 int signModifier
= 0;
404 for (outputCount
= 0; outputCount
< outputSize
; outputCount
++)
406 int32_t decodedValue
;
410 k
= 31 - rice_kmodifier
- count_leading_zeros((history
>> 9) + 3);
412 if (k
< 0) k
+= rice_kmodifier
;
413 else k
= rice_kmodifier
;
415 // note: don't use rice_kmodifier_mask here (set mask to 0xFFFFFFFF)
416 decodedValue
= entropy_decode_value(alac
, readSampleSize
, k
, 0xFFFFFFFF);
418 decodedValue
+= signModifier
;
419 finalValue
= (decodedValue
+ 1) / 2; // inc by 1 and shift out sign bit
420 if (decodedValue
& 1) // the sign is stored in the low bit
423 outputBuffer
[outputCount
] = finalValue
;
428 history
+= (decodedValue
* rice_historymult
)
429 - ((history
* rice_historymult
) >> 9);
431 if (decodedValue
> 0xFFFF)
434 // special case, for compressed blocks of 0
435 if ((history
< 128) && (outputCount
+ 1 < outputSize
))
441 k
= count_leading_zeros(history
) + ((history
+ 16) / 64) - 24;
443 // note: blockSize is always 16bit
444 blockSize
= entropy_decode_value(alac
, 16, k
, rice_kmodifier_mask
);
449 memset(&outputBuffer
[outputCount
+ 1], 0, blockSize
* sizeof(*outputBuffer
));
450 outputCount
+= blockSize
;
453 if (blockSize
> 0xFFFF)
461 #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
463 #define SIGN_ONLY(v) \
468 static void predictor_decompress_fir_adapt(int32_t *error_buffer
,
472 int16_t *predictor_coef_table
,
473 int predictor_coef_num
,
474 int predictor_quantitization
)
478 /* first sample always copies */
479 *buffer_out
= *error_buffer
;
481 if (!predictor_coef_num
)
483 if (output_size
<= 1) return;
484 memcpy(buffer_out
+1, error_buffer
+1, (output_size
-1) * 4);
488 if (predictor_coef_num
== 0x1f) /* 11111 - max value of predictor_coef_num */
489 { /* second-best case scenario for fir decompression,
490 * error describes a small difference from the previous sample only
492 if (output_size
<= 1) return;
493 for (i
= 0; i
< output_size
- 1; i
++)
498 prev_value
= buffer_out
[i
];
499 error_value
= error_buffer
[i
+1];
500 buffer_out
[i
+1] = SIGN_EXTENDED32((prev_value
+ error_value
), readsamplesize
);
505 /* read warm-up samples */
506 if (predictor_coef_num
> 0)
509 for (i
= 0; i
< predictor_coef_num
; i
++)
513 val
= buffer_out
[i
] + error_buffer
[i
+1];
515 val
= SIGN_EXTENDED32(val
, readsamplesize
);
517 buffer_out
[i
+1] = val
;
522 /* 4 and 8 are very common cases (the only ones i've seen). these
523 * should be unrolled and optimised
525 if (predictor_coef_num
== 4)
527 /* FIXME: optimised general case */
531 if (predictor_coef_table
== 8)
533 /* FIXME: optimised general case */
540 if (predictor_coef_num
> 0)
542 for (i
= predictor_coef_num
+ 1;
549 int error_val
= error_buffer
[i
];
551 for (j
= 0; j
< predictor_coef_num
; j
++)
553 sum
+= (buffer_out
[predictor_coef_num
-j
] - buffer_out
[0]) *
554 predictor_coef_table
[j
];
557 outval
= (1 << (predictor_quantitization
-1)) + sum
;
558 outval
= outval
>> predictor_quantitization
;
559 outval
= outval
+ buffer_out
[0] + error_val
;
560 outval
= SIGN_EXTENDED32(outval
, readsamplesize
);
562 buffer_out
[predictor_coef_num
+1] = outval
;
566 int predictor_num
= predictor_coef_num
- 1;
568 while (predictor_num
>= 0 && error_val
> 0)
570 int val
= buffer_out
[0] - buffer_out
[predictor_coef_num
- predictor_num
];
571 int sign
= SIGN_ONLY(val
);
573 predictor_coef_table
[predictor_num
] -= sign
;
575 val
*= sign
; /* absolute value */
577 error_val
-= ((val
>> predictor_quantitization
) *
578 (predictor_coef_num
- predictor_num
));
583 else if (error_val
< 0)
585 int predictor_num
= predictor_coef_num
- 1;
587 while (predictor_num
>= 0 && error_val
< 0)
589 int val
= buffer_out
[0] - buffer_out
[predictor_coef_num
- predictor_num
];
590 int sign
= - SIGN_ONLY(val
);
592 predictor_coef_table
[predictor_num
] -= sign
;
594 val
*= sign
; /* neg value */
596 error_val
-= ((val
>> predictor_quantitization
) *
597 (predictor_coef_num
- predictor_num
));
608 void deinterlace_16(int32_t *buffer_a
, int32_t *buffer_b
,
610 int numchannels
, int numsamples
,
611 uint8_t interlacing_shift
,
612 uint8_t interlacing_leftweight
)
615 if (numsamples
<= 0) return;
617 /* weighted interlacing */
618 if (interlacing_leftweight
)
620 for (i
= 0; i
< numsamples
; i
++)
622 int32_t difference
, midright
;
626 midright
= buffer_a
[i
];
627 difference
= buffer_b
[i
];
630 right
= midright
- ((difference
* interlacing_leftweight
) >> interlacing_shift
);
631 left
= right
+ difference
;
633 /* output is always little endian */
640 buffer_out
[i
*numchannels
] = left
;
641 buffer_out
[i
*numchannels
+ 1] = right
;
647 /* otherwise basic interlacing took place */
648 for (i
= 0; i
< numsamples
; i
++)
655 /* output is always little endian */
662 buffer_out
[i
*numchannels
] = left
;
663 buffer_out
[i
*numchannels
+ 1] = right
;
667 void deinterlace_24(int32_t *buffer_a
, int32_t *buffer_b
,
668 int uncompressed_bytes
,
669 int32_t *uncompressed_bytes_buffer_a
, int32_t *uncompressed_bytes_buffer_b
,
671 int numchannels
, int numsamples
,
672 uint8_t interlacing_shift
,
673 uint8_t interlacing_leftweight
)
676 if (numsamples
<= 0) return;
678 /* weighted interlacing */
679 if (interlacing_leftweight
)
681 for (i
= 0; i
< numsamples
; i
++)
683 int32_t difference
, midright
;
687 midright
= buffer_a
[i
];
688 difference
= buffer_b
[i
];
690 right
= midright
- ((difference
* interlacing_leftweight
) >> interlacing_shift
);
691 left
= right
+ difference
;
693 if (uncompressed_bytes
)
695 uint32_t mask
= ~(0xFFFFFFFF << (uncompressed_bytes
* 8));
696 left
<<= (uncompressed_bytes
* 8);
697 right
<<= (uncompressed_bytes
* 8);
699 left
|= uncompressed_bytes_buffer_a
[i
] & mask
;
700 right
|= uncompressed_bytes_buffer_b
[i
] & mask
;
703 ((uint8_t*)buffer_out
)[i
* numchannels
* 3] = (left
) & 0xFF;
704 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 1] = (left
>> 8) & 0xFF;
705 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 2] = (left
>> 16) & 0xFF;
707 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 3] = (right
) & 0xFF;
708 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 4] = (right
>> 8) & 0xFF;
709 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 5] = (right
>> 16) & 0xFF;
715 /* otherwise basic interlacing took place */
716 for (i
= 0; i
< numsamples
; i
++)
723 if (uncompressed_bytes
)
725 uint32_t mask
= ~(0xFFFFFFFF << (uncompressed_bytes
* 8));
726 left
<<= (uncompressed_bytes
* 8);
727 right
<<= (uncompressed_bytes
* 8);
729 left
|= uncompressed_bytes_buffer_a
[i
] & mask
;
730 right
|= uncompressed_bytes_buffer_b
[i
] & mask
;
733 ((uint8_t*)buffer_out
)[i
* numchannels
* 3] = (left
) & 0xFF;
734 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 1] = (left
>> 8) & 0xFF;
735 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 2] = (left
>> 16) & 0xFF;
737 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 3] = (right
) & 0xFF;
738 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 4] = (right
>> 8) & 0xFF;
739 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 5] = (right
>> 16) & 0xFF;
745 void decode_frame(alac_file
*alac
,
746 unsigned char *inbuffer
,
747 void *outbuffer
, int *outputsize
)
750 int32_t outputsamples
= alac
->setinfo_max_samples_per_frame
;
752 /* setup the stream */
753 alac
->input_buffer
= inbuffer
;
754 alac
->input_buffer_bitaccumulator
= 0;
756 channels
= readbits(alac
, 3);
758 *outputsize
= outputsamples
* alac
->bytespersample
;
762 case 0: /* 1 channel */
768 int uncompressed_bytes
;
771 /* 2^result = something to do with output waiting.
772 * perhaps matters if we read > 1 frame in a pass?
776 readbits(alac
, 12); /* unknown, skip 12 bits */
778 hassize
= readbits(alac
, 1); /* the output sample size is stored soon */
780 uncompressed_bytes
= readbits(alac
, 2); /* number of bytes in the (compressed) stream that are not compressed */
782 isnotcompressed
= readbits(alac
, 1); /* whether the frame is compressed */
786 /* now read the number of samples,
787 * as a 32bit integer */
788 outputsamples
= readbits(alac
, 32);
789 *outputsize
= outputsamples
* alac
->bytespersample
;
792 readsamplesize
= alac
->setinfo_sample_size
- (uncompressed_bytes
* 8);
794 if (!isnotcompressed
)
795 { /* so it is compressed */
796 int16_t predictor_coef_table
[32];
797 int predictor_coef_num
;
799 int prediction_quantitization
;
802 /* skip 16 bits, not sure what they are. seem to be used in
803 * two channel case */
807 prediction_type
= readbits(alac
, 4);
808 prediction_quantitization
= readbits(alac
, 4);
810 ricemodifier
= readbits(alac
, 3);
811 predictor_coef_num
= readbits(alac
, 5);
813 /* read the predictor table */
814 for (i
= 0; i
< predictor_coef_num
; i
++)
816 predictor_coef_table
[i
] = (int16_t)readbits(alac
, 16);
819 if (uncompressed_bytes
)
822 for (i
= 0; i
< outputsamples
; i
++)
824 alac
->uncompressed_bytes_buffer_a
[i
] = readbits(alac
, uncompressed_bytes
* 8);
828 entropy_rice_decode(alac
,
829 alac
->predicterror_buffer_a
,
832 alac
->setinfo_rice_initialhistory
,
833 alac
->setinfo_rice_kmodifier
,
834 ricemodifier
* alac
->setinfo_rice_historymult
/ 4,
835 (1 << alac
->setinfo_rice_kmodifier
) - 1);
837 if (prediction_type
== 0)
839 predictor_decompress_fir_adapt(alac
->predicterror_buffer_a
,
840 alac
->outputsamples_buffer_a
,
843 predictor_coef_table
,
845 prediction_quantitization
);
849 fprintf(stderr
, "FIXME: unhandled predicition type: %i\n", prediction_type
);
850 /* i think the only other prediction type (or perhaps this is just a
851 * boolean?) runs adaptive fir twice.. like:
852 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
853 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
860 { /* not compressed, easy case */
861 if (alac
->setinfo_sample_size
<= 16)
864 for (i
= 0; i
< outputsamples
; i
++)
866 int32_t audiobits
= readbits(alac
, alac
->setinfo_sample_size
);
868 audiobits
= SIGN_EXTENDED32(audiobits
, alac
->setinfo_sample_size
);
870 alac
->outputsamples_buffer_a
[i
] = audiobits
;
876 for (i
= 0; i
< outputsamples
; i
++)
880 audiobits
= readbits(alac
, 16);
881 /* special case of sign extension..
882 * as we'll be ORing the low 16bits into this */
883 audiobits
= audiobits
<< (alac
->setinfo_sample_size
- 16);
884 audiobits
|= readbits(alac
, alac
->setinfo_sample_size
- 16);
885 audiobits
= SignExtend24(audiobits
);
887 alac
->outputsamples_buffer_a
[i
] = audiobits
;
890 uncompressed_bytes
= 0; // always 0 for uncompressed
893 switch(alac
->setinfo_sample_size
)
898 for (i
= 0; i
< outputsamples
; i
++)
900 int16_t sample
= alac
->outputsamples_buffer_a
[i
];
903 ((int16_t*)outbuffer
)[i
* alac
->numchannels
] = sample
;
910 for (i
= 0; i
< outputsamples
; i
++)
912 int32_t sample
= alac
->outputsamples_buffer_a
[i
];
914 if (uncompressed_bytes
)
917 sample
= sample
<< (uncompressed_bytes
* 8);
918 mask
= ~(0xFFFFFFFF << (uncompressed_bytes
* 8));
919 sample
|= alac
->uncompressed_bytes_buffer_a
[i
] & mask
;
922 ((uint8_t*)outbuffer
)[i
* alac
->numchannels
* 3] = (sample
) & 0xFF;
923 ((uint8_t*)outbuffer
)[i
* alac
->numchannels
* 3 + 1] = (sample
>> 8) & 0xFF;
924 ((uint8_t*)outbuffer
)[i
* alac
->numchannels
* 3 + 2] = (sample
>> 16) & 0xFF;
930 fprintf(stderr
, "FIXME: unimplemented sample size %i\n", alac
->setinfo_sample_size
);
937 case 1: /* 2 channels */
943 int uncompressed_bytes
;
945 uint8_t interlacing_shift
;
946 uint8_t interlacing_leftweight
;
948 /* 2^result = something to do with output waiting.
949 * perhaps matters if we read > 1 frame in a pass?
953 readbits(alac
, 12); /* unknown, skip 12 bits */
955 hassize
= readbits(alac
, 1); /* the output sample size is stored soon */
957 uncompressed_bytes
= readbits(alac
, 2); /* the number of bytes in the (compressed) stream that are not compressed */
959 isnotcompressed
= readbits(alac
, 1); /* whether the frame is compressed */
963 /* now read the number of samples,
964 * as a 32bit integer */
965 outputsamples
= readbits(alac
, 32);
966 *outputsize
= outputsamples
* alac
->bytespersample
;
969 readsamplesize
= alac
->setinfo_sample_size
- (uncompressed_bytes
* 8) + 1;
971 if (!isnotcompressed
)
973 int16_t predictor_coef_table_a
[32];
974 int predictor_coef_num_a
;
975 int prediction_type_a
;
976 int prediction_quantitization_a
;
979 int16_t predictor_coef_table_b
[32];
980 int predictor_coef_num_b
;
981 int prediction_type_b
;
982 int prediction_quantitization_b
;
987 interlacing_shift
= readbits(alac
, 8);
988 interlacing_leftweight
= readbits(alac
, 8);
990 /******** channel 1 ***********/
991 prediction_type_a
= readbits(alac
, 4);
992 prediction_quantitization_a
= readbits(alac
, 4);
994 ricemodifier_a
= readbits(alac
, 3);
995 predictor_coef_num_a
= readbits(alac
, 5);
997 /* read the predictor table */
998 for (i
= 0; i
< predictor_coef_num_a
; i
++)
1000 predictor_coef_table_a
[i
] = (int16_t)readbits(alac
, 16);
1003 /******** channel 2 *********/
1004 prediction_type_b
= readbits(alac
, 4);
1005 prediction_quantitization_b
= readbits(alac
, 4);
1007 ricemodifier_b
= readbits(alac
, 3);
1008 predictor_coef_num_b
= readbits(alac
, 5);
1010 /* read the predictor table */
1011 for (i
= 0; i
< predictor_coef_num_b
; i
++)
1013 predictor_coef_table_b
[i
] = (int16_t)readbits(alac
, 16);
1016 /*********************/
1017 if (uncompressed_bytes
)
1018 { /* see mono case */
1020 for (i
= 0; i
< outputsamples
; i
++)
1022 alac
->uncompressed_bytes_buffer_a
[i
] = readbits(alac
, uncompressed_bytes
* 8);
1023 alac
->uncompressed_bytes_buffer_b
[i
] = readbits(alac
, uncompressed_bytes
* 8);
1028 entropy_rice_decode(alac
,
1029 alac
->predicterror_buffer_a
,
1032 alac
->setinfo_rice_initialhistory
,
1033 alac
->setinfo_rice_kmodifier
,
1034 ricemodifier_a
* alac
->setinfo_rice_historymult
/ 4,
1035 (1 << alac
->setinfo_rice_kmodifier
) - 1);
1037 if (prediction_type_a
== 0)
1038 { /* adaptive fir */
1039 predictor_decompress_fir_adapt(alac
->predicterror_buffer_a
,
1040 alac
->outputsamples_buffer_a
,
1043 predictor_coef_table_a
,
1044 predictor_coef_num_a
,
1045 prediction_quantitization_a
);
1048 { /* see mono case */
1049 fprintf(stderr
, "FIXME: unhandled predicition type: %i\n", prediction_type_a
);
1053 entropy_rice_decode(alac
,
1054 alac
->predicterror_buffer_b
,
1057 alac
->setinfo_rice_initialhistory
,
1058 alac
->setinfo_rice_kmodifier
,
1059 ricemodifier_b
* alac
->setinfo_rice_historymult
/ 4,
1060 (1 << alac
->setinfo_rice_kmodifier
) - 1);
1062 if (prediction_type_b
== 0)
1063 { /* adaptive fir */
1064 predictor_decompress_fir_adapt(alac
->predicterror_buffer_b
,
1065 alac
->outputsamples_buffer_b
,
1068 predictor_coef_table_b
,
1069 predictor_coef_num_b
,
1070 prediction_quantitization_b
);
1074 fprintf(stderr
, "FIXME: unhandled predicition type: %i\n", prediction_type_b
);
1078 { /* not compressed, easy case */
1079 if (alac
->setinfo_sample_size
<= 16)
1082 for (i
= 0; i
< outputsamples
; i
++)
1084 int32_t audiobits_a
, audiobits_b
;
1086 audiobits_a
= readbits(alac
, alac
->setinfo_sample_size
);
1087 audiobits_b
= readbits(alac
, alac
->setinfo_sample_size
);
1089 audiobits_a
= SIGN_EXTENDED32(audiobits_a
, alac
->setinfo_sample_size
);
1090 audiobits_b
= SIGN_EXTENDED32(audiobits_b
, alac
->setinfo_sample_size
);
1092 alac
->outputsamples_buffer_a
[i
] = audiobits_a
;
1093 alac
->outputsamples_buffer_b
[i
] = audiobits_b
;
1099 for (i
= 0; i
< outputsamples
; i
++)
1101 int32_t audiobits_a
, audiobits_b
;
1103 audiobits_a
= readbits(alac
, 16);
1104 audiobits_a
= audiobits_a
<< (alac
->setinfo_sample_size
- 16);
1105 audiobits_a
|= readbits(alac
, alac
->setinfo_sample_size
- 16);
1106 audiobits_a
= SignExtend24(audiobits_a
);
1108 audiobits_b
= readbits(alac
, 16);
1109 audiobits_b
= audiobits_b
<< (alac
->setinfo_sample_size
- 16);
1110 audiobits_b
|= readbits(alac
, alac
->setinfo_sample_size
- 16);
1111 audiobits_b
= SignExtend24(audiobits_b
);
1113 alac
->outputsamples_buffer_a
[i
] = audiobits_a
;
1114 alac
->outputsamples_buffer_b
[i
] = audiobits_b
;
1117 uncompressed_bytes
= 0; // always 0 for uncompressed
1118 interlacing_shift
= 0;
1119 interlacing_leftweight
= 0;
1122 switch(alac
->setinfo_sample_size
)
1126 deinterlace_16(alac
->outputsamples_buffer_a
,
1127 alac
->outputsamples_buffer_b
,
1128 (int16_t*)outbuffer
,
1132 interlacing_leftweight
);
1137 deinterlace_24(alac
->outputsamples_buffer_a
,
1138 alac
->outputsamples_buffer_b
,
1140 alac
->uncompressed_bytes_buffer_a
,
1141 alac
->uncompressed_bytes_buffer_b
,
1142 (int16_t*)outbuffer
,
1146 interlacing_leftweight
);
1151 fprintf(stderr
, "FIXME: unimplemented sample size %i\n", alac
->setinfo_sample_size
);
1162 alac_file
*create_alac(int samplesize
, int numchannels
)
1164 alac_file
*newfile
= malloc(sizeof(alac_file
));
1166 newfile
->samplesize
= samplesize
;
1167 newfile
->numchannels
= numchannels
;
1168 newfile
->bytespersample
= (samplesize
/ 8) * numchannels
;