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 static void deallocate_buffers(alac_file
*alac
)
116 free(alac
->predicterror_buffer_a
);
117 free(alac
->predicterror_buffer_b
);
118 alac
->predicterror_buffer_a
= NULL
;
119 alac
->predicterror_buffer_b
= NULL
;
121 free(alac
->outputsamples_buffer_a
);
122 free(alac
->outputsamples_buffer_b
);
123 alac
->outputsamples_buffer_a
= NULL
;
124 alac
->outputsamples_buffer_b
= NULL
;
126 free(alac
->uncompressed_bytes_buffer_a
);
127 free(alac
->uncompressed_bytes_buffer_b
);
128 alac
->uncompressed_bytes_buffer_a
= NULL
;
129 alac
->uncompressed_bytes_buffer_b
= NULL
;
132 void alac_set_info(alac_file
*alac
, char *inputbuffer
)
134 char *ptr
= inputbuffer
;
143 alac
->setinfo_max_samples_per_frame
= *(uint32_t*)ptr
; /* buffer size / 2 ? */
145 _Swap32(alac
->setinfo_max_samples_per_frame
);
147 alac
->setinfo_7a
= *(uint8_t*)ptr
;
149 alac
->setinfo_sample_size
= *(uint8_t*)ptr
;
151 alac
->setinfo_rice_historymult
= *(uint8_t*)ptr
;
153 alac
->setinfo_rice_initialhistory
= *(uint8_t*)ptr
;
155 alac
->setinfo_rice_kmodifier
= *(uint8_t*)ptr
;
157 alac
->setinfo_7f
= *(uint8_t*)ptr
;
159 alac
->setinfo_80
= *(uint16_t*)ptr
;
161 _Swap16(alac
->setinfo_80
);
163 alac
->setinfo_82
= *(uint32_t*)ptr
;
165 _Swap32(alac
->setinfo_82
);
167 alac
->setinfo_86
= *(uint32_t*)ptr
;
169 _Swap32(alac
->setinfo_86
);
171 alac
->setinfo_8a_rate
= *(uint32_t*)ptr
;
173 _Swap32(alac
->setinfo_8a_rate
);
176 allocate_buffers(alac
);
182 /* supports reading 1 to 16 bits, in big endian format */
183 static uint32_t readbits_16(alac_file
*alac
, int bits
)
188 result
= (alac
->input_buffer
[0] << 16) |
189 (alac
->input_buffer
[1] << 8) |
190 (alac
->input_buffer
[2]);
192 /* shift left by the number of bits we've already read,
193 * so that the top 'n' bits of the 24 bits we read will
194 * be the return bits */
195 result
= result
<< alac
->input_buffer_bitaccumulator
;
197 result
= result
& 0x00ffffff;
199 /* and then only want the top 'n' bits from that, where
201 result
= result
>> (24 - bits
);
203 new_accumulator
= (alac
->input_buffer_bitaccumulator
+ bits
);
205 /* increase the buffer pointer if we've read over n bytes. */
206 alac
->input_buffer
+= (new_accumulator
>> 3);
208 /* and the remainder goes back into the bit accumulator */
209 alac
->input_buffer_bitaccumulator
= (new_accumulator
& 7);
214 /* supports reading 1 to 32 bits, in big endian format */
215 static uint32_t readbits(alac_file
*alac
, int bits
)
222 result
= readbits_16(alac
, 16) << bits
;
225 result
|= readbits_16(alac
, bits
);
230 /* reads a single bit */
231 static int readbit(alac_file
*alac
)
236 result
= alac
->input_buffer
[0];
238 result
= result
<< alac
->input_buffer_bitaccumulator
;
240 result
= result
>> 7 & 1;
242 new_accumulator
= (alac
->input_buffer_bitaccumulator
+ 1);
244 alac
->input_buffer
+= (new_accumulator
/ 8);
246 alac
->input_buffer_bitaccumulator
= (new_accumulator
% 8);
251 static void unreadbits(alac_file
*alac
, int bits
)
253 int new_accumulator
= (alac
->input_buffer_bitaccumulator
- bits
);
255 alac
->input_buffer
+= (new_accumulator
>> 3);
257 alac
->input_buffer_bitaccumulator
= (new_accumulator
& 7);
258 if (alac
->input_buffer_bitaccumulator
< 0)
259 alac
->input_buffer_bitaccumulator
*= -1;
262 /* various implementations of count_leading_zero:
263 * the first one is the original one, the simplest and most
264 * obvious for what it's doing. never use this.
265 * then there are the asm ones. fill in as necessary
266 * and finally an unrolled and optimised c version
270 /* hideously inefficient. could use a bitmask search,
271 * alternatively bsr on x86,
273 static int count_leading_zeros(int32_t input
)
276 while (!(0x80000000 & input
) && i
< 32)
283 #elif defined(__GNUC__) && (defined(_X86) || defined(__i386) || defined(i386))
284 /* for some reason the unrolled version (below) is
285 * actually faster than this. yay intel!
287 static int count_leading_zeros(int input
)
290 if (!input
) return 32;
294 return (0x1f - output
);
296 #elif defined(__GNUC__)
297 static int count_leading_zeros(int input
)
299 return __builtin_clz(input
);
301 #elif defined(_MSC_VER) && defined(_M_IX86)
302 static int count_leading_zeros(int input
)
305 if (!input
) return 32;
317 #warning using generic count leading zeroes. You may wish to write one for your CPU / compiler
318 static int count_leading_zeros(int input
)
323 curbyte
= input
>> 24;
324 if (curbyte
) goto found
;
327 curbyte
= input
>> 16;
328 if (curbyte
& 0xff) goto found
;
331 curbyte
= input
>> 8;
332 if (curbyte
& 0xff) goto found
;
336 if (curbyte
& 0xff) goto found
;
342 if (!(curbyte
& 0xf0))
358 /* shouldn't get here: */
363 #define RICE_THRESHOLD 8 // maximum number of bits for a rice prefix.
365 int32_t entropy_decode_value(alac_file
* alac
,
368 int rice_kmodifier_mask
)
370 int32_t x
= 0; // decoded value
372 // read x, number of 1s before 0 represent the rice value.
373 while (x
<= RICE_THRESHOLD
&& readbit(alac
))
378 if (x
> RICE_THRESHOLD
)
380 // read the number from the bit stream (raw value)
383 value
= readbits(alac
, readSampleSize
);
386 value
&= (((uint32_t)0xffffffff) >> (32 - readSampleSize
));
394 int extraBits
= readbits(alac
, k
);
397 x
*= (((1 << k
) - 1) & rice_kmodifier_mask
);
409 void entropy_rice_decode(alac_file
* alac
,
410 int32_t* outputBuffer
,
413 int rice_initialhistory
,
415 int rice_historymult
,
416 int rice_kmodifier_mask
)
419 int history
= rice_initialhistory
;
420 int signModifier
= 0;
422 for (outputCount
= 0; outputCount
< outputSize
; outputCount
++)
424 int32_t decodedValue
;
428 k
= 31 - rice_kmodifier
- count_leading_zeros((history
>> 9) + 3);
430 if (k
< 0) k
+= rice_kmodifier
;
431 else k
= rice_kmodifier
;
433 // note: don't use rice_kmodifier_mask here (set mask to 0xFFFFFFFF)
434 decodedValue
= entropy_decode_value(alac
, readSampleSize
, k
, 0xFFFFFFFF);
436 decodedValue
+= signModifier
;
437 finalValue
= (decodedValue
+ 1) / 2; // inc by 1 and shift out sign bit
438 if (decodedValue
& 1) // the sign is stored in the low bit
441 outputBuffer
[outputCount
] = finalValue
;
446 history
+= (decodedValue
* rice_historymult
)
447 - ((history
* rice_historymult
) >> 9);
449 if (decodedValue
> 0xFFFF)
452 // special case, for compressed blocks of 0
453 if ((history
< 128) && (outputCount
+ 1 < outputSize
))
459 k
= count_leading_zeros(history
) + ((history
+ 16) / 64) - 24;
461 // note: blockSize is always 16bit
462 blockSize
= entropy_decode_value(alac
, 16, k
, rice_kmodifier_mask
);
467 memset(&outputBuffer
[outputCount
+ 1], 0, blockSize
* sizeof(*outputBuffer
));
468 outputCount
+= blockSize
;
471 if (blockSize
> 0xFFFF)
479 #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
481 #define SIGN_ONLY(v) \
486 static void predictor_decompress_fir_adapt(int32_t *error_buffer
,
490 int16_t *predictor_coef_table
,
491 int predictor_coef_num
,
492 int predictor_quantitization
)
496 /* first sample always copies */
497 *buffer_out
= *error_buffer
;
499 if (!predictor_coef_num
)
501 if (output_size
<= 1) return;
502 memcpy(buffer_out
+1, error_buffer
+1, (output_size
-1) * 4);
506 if (predictor_coef_num
== 0x1f) /* 11111 - max value of predictor_coef_num */
507 { /* second-best case scenario for fir decompression,
508 * error describes a small difference from the previous sample only
510 if (output_size
<= 1) return;
511 for (i
= 0; i
< output_size
- 1; i
++)
516 prev_value
= buffer_out
[i
];
517 error_value
= error_buffer
[i
+1];
518 buffer_out
[i
+1] = SIGN_EXTENDED32((prev_value
+ error_value
), readsamplesize
);
523 /* read warm-up samples */
524 if (predictor_coef_num
> 0)
527 for (i
= 0; i
< predictor_coef_num
; i
++)
531 val
= buffer_out
[i
] + error_buffer
[i
+1];
533 val
= SIGN_EXTENDED32(val
, readsamplesize
);
535 buffer_out
[i
+1] = val
;
540 /* 4 and 8 are very common cases (the only ones i've seen). these
541 * should be unrolled and optimised
543 if (predictor_coef_num
== 4)
545 /* FIXME: optimised general case */
549 if (predictor_coef_table
== 8)
551 /* FIXME: optimised general case */
558 if (predictor_coef_num
> 0)
560 for (i
= predictor_coef_num
+ 1;
567 int error_val
= error_buffer
[i
];
569 for (j
= 0; j
< predictor_coef_num
; j
++)
571 sum
+= (buffer_out
[predictor_coef_num
-j
] - buffer_out
[0]) *
572 predictor_coef_table
[j
];
575 outval
= (1 << (predictor_quantitization
-1)) + sum
;
576 outval
= outval
>> predictor_quantitization
;
577 outval
= outval
+ buffer_out
[0] + error_val
;
578 outval
= SIGN_EXTENDED32(outval
, readsamplesize
);
580 buffer_out
[predictor_coef_num
+1] = outval
;
584 int predictor_num
= predictor_coef_num
- 1;
586 while (predictor_num
>= 0 && error_val
> 0)
588 int val
= buffer_out
[0] - buffer_out
[predictor_coef_num
- predictor_num
];
589 int sign
= SIGN_ONLY(val
);
591 predictor_coef_table
[predictor_num
] -= sign
;
593 val
*= sign
; /* absolute value */
595 error_val
-= ((val
>> predictor_quantitization
) *
596 (predictor_coef_num
- predictor_num
));
601 else if (error_val
< 0)
603 int predictor_num
= predictor_coef_num
- 1;
605 while (predictor_num
>= 0 && error_val
< 0)
607 int val
= buffer_out
[0] - buffer_out
[predictor_coef_num
- predictor_num
];
608 int sign
= - SIGN_ONLY(val
);
610 predictor_coef_table
[predictor_num
] -= sign
;
612 val
*= sign
; /* neg value */
614 error_val
-= ((val
>> predictor_quantitization
) *
615 (predictor_coef_num
- predictor_num
));
626 void deinterlace_16(int32_t *buffer_a
, int32_t *buffer_b
,
628 int numchannels
, int numsamples
,
629 uint8_t interlacing_shift
,
630 uint8_t interlacing_leftweight
)
633 if (numsamples
<= 0) return;
635 /* weighted interlacing */
636 if (interlacing_leftweight
)
638 for (i
= 0; i
< numsamples
; i
++)
640 int32_t difference
, midright
;
644 midright
= buffer_a
[i
];
645 difference
= buffer_b
[i
];
648 right
= midright
- ((difference
* interlacing_leftweight
) >> interlacing_shift
);
649 left
= right
+ difference
;
651 /* output is always little endian */
658 buffer_out
[i
*numchannels
] = left
;
659 buffer_out
[i
*numchannels
+ 1] = right
;
665 /* otherwise basic interlacing took place */
666 for (i
= 0; i
< numsamples
; i
++)
673 /* output is always little endian */
680 buffer_out
[i
*numchannels
] = left
;
681 buffer_out
[i
*numchannels
+ 1] = right
;
685 void deinterlace_24(int32_t *buffer_a
, int32_t *buffer_b
,
686 int uncompressed_bytes
,
687 int32_t *uncompressed_bytes_buffer_a
, int32_t *uncompressed_bytes_buffer_b
,
689 int numchannels
, int numsamples
,
690 uint8_t interlacing_shift
,
691 uint8_t interlacing_leftweight
)
694 if (numsamples
<= 0) return;
696 /* weighted interlacing */
697 if (interlacing_leftweight
)
699 for (i
= 0; i
< numsamples
; i
++)
701 int32_t difference
, midright
;
705 midright
= buffer_a
[i
];
706 difference
= buffer_b
[i
];
708 right
= midright
- ((difference
* interlacing_leftweight
) >> interlacing_shift
);
709 left
= right
+ difference
;
711 if (uncompressed_bytes
)
713 uint32_t mask
= ~(0xFFFFFFFF << (uncompressed_bytes
* 8));
714 left
<<= (uncompressed_bytes
* 8);
715 right
<<= (uncompressed_bytes
* 8);
717 left
|= uncompressed_bytes_buffer_a
[i
] & mask
;
718 right
|= uncompressed_bytes_buffer_b
[i
] & mask
;
721 ((uint8_t*)buffer_out
)[i
* numchannels
* 3] = (left
) & 0xFF;
722 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 1] = (left
>> 8) & 0xFF;
723 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 2] = (left
>> 16) & 0xFF;
725 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 3] = (right
) & 0xFF;
726 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 4] = (right
>> 8) & 0xFF;
727 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 5] = (right
>> 16) & 0xFF;
733 /* otherwise basic interlacing took place */
734 for (i
= 0; i
< numsamples
; i
++)
741 if (uncompressed_bytes
)
743 uint32_t mask
= ~(0xFFFFFFFF << (uncompressed_bytes
* 8));
744 left
<<= (uncompressed_bytes
* 8);
745 right
<<= (uncompressed_bytes
* 8);
747 left
|= uncompressed_bytes_buffer_a
[i
] & mask
;
748 right
|= uncompressed_bytes_buffer_b
[i
] & mask
;
751 ((uint8_t*)buffer_out
)[i
* numchannels
* 3] = (left
) & 0xFF;
752 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 1] = (left
>> 8) & 0xFF;
753 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 2] = (left
>> 16) & 0xFF;
755 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 3] = (right
) & 0xFF;
756 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 4] = (right
>> 8) & 0xFF;
757 ((uint8_t*)buffer_out
)[i
* numchannels
* 3 + 5] = (right
>> 16) & 0xFF;
763 void decode_frame(alac_file
*alac
,
764 unsigned char *inbuffer
,
765 void *outbuffer
, int *outputsize
)
768 int32_t outputsamples
= alac
->setinfo_max_samples_per_frame
;
770 /* setup the stream */
771 alac
->input_buffer
= inbuffer
;
772 alac
->input_buffer_bitaccumulator
= 0;
774 channels
= readbits(alac
, 3);
776 *outputsize
= outputsamples
* alac
->bytespersample
;
780 case 0: /* 1 channel */
786 int uncompressed_bytes
;
789 /* 2^result = something to do with output waiting.
790 * perhaps matters if we read > 1 frame in a pass?
794 readbits(alac
, 12); /* unknown, skip 12 bits */
796 hassize
= readbits(alac
, 1); /* the output sample size is stored soon */
798 uncompressed_bytes
= readbits(alac
, 2); /* number of bytes in the (compressed) stream that are not compressed */
800 isnotcompressed
= readbits(alac
, 1); /* whether the frame is compressed */
804 /* now read the number of samples,
805 * as a 32bit integer */
806 outputsamples
= readbits(alac
, 32);
807 *outputsize
= outputsamples
* alac
->bytespersample
;
810 readsamplesize
= alac
->setinfo_sample_size
- (uncompressed_bytes
* 8);
812 if (!isnotcompressed
)
813 { /* so it is compressed */
814 int16_t predictor_coef_table
[32];
815 int predictor_coef_num
;
817 int prediction_quantitization
;
820 /* skip 16 bits, not sure what they are. seem to be used in
821 * two channel case */
825 prediction_type
= readbits(alac
, 4);
826 prediction_quantitization
= readbits(alac
, 4);
828 ricemodifier
= readbits(alac
, 3);
829 predictor_coef_num
= readbits(alac
, 5);
831 /* read the predictor table */
832 for (i
= 0; i
< predictor_coef_num
; i
++)
834 predictor_coef_table
[i
] = (int16_t)readbits(alac
, 16);
837 if (uncompressed_bytes
)
840 for (i
= 0; i
< outputsamples
; i
++)
842 alac
->uncompressed_bytes_buffer_a
[i
] = readbits(alac
, uncompressed_bytes
* 8);
846 entropy_rice_decode(alac
,
847 alac
->predicterror_buffer_a
,
850 alac
->setinfo_rice_initialhistory
,
851 alac
->setinfo_rice_kmodifier
,
852 ricemodifier
* alac
->setinfo_rice_historymult
/ 4,
853 (1 << alac
->setinfo_rice_kmodifier
) - 1);
855 if (prediction_type
== 0)
857 predictor_decompress_fir_adapt(alac
->predicterror_buffer_a
,
858 alac
->outputsamples_buffer_a
,
861 predictor_coef_table
,
863 prediction_quantitization
);
867 fprintf(stderr
, "FIXME: unhandled predicition type: %i\n", prediction_type
);
868 /* i think the only other prediction type (or perhaps this is just a
869 * boolean?) runs adaptive fir twice.. like:
870 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
871 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
878 { /* not compressed, easy case */
879 if (alac
->setinfo_sample_size
<= 16)
882 for (i
= 0; i
< outputsamples
; i
++)
884 int32_t audiobits
= readbits(alac
, alac
->setinfo_sample_size
);
886 audiobits
= SIGN_EXTENDED32(audiobits
, alac
->setinfo_sample_size
);
888 alac
->outputsamples_buffer_a
[i
] = audiobits
;
894 for (i
= 0; i
< outputsamples
; i
++)
898 audiobits
= readbits(alac
, 16);
899 /* special case of sign extension..
900 * as we'll be ORing the low 16bits into this */
901 audiobits
= audiobits
<< (alac
->setinfo_sample_size
- 16);
902 audiobits
|= readbits(alac
, alac
->setinfo_sample_size
- 16);
903 audiobits
= SignExtend24(audiobits
);
905 alac
->outputsamples_buffer_a
[i
] = audiobits
;
908 uncompressed_bytes
= 0; // always 0 for uncompressed
911 switch(alac
->setinfo_sample_size
)
916 for (i
= 0; i
< outputsamples
; i
++)
918 int16_t sample
= alac
->outputsamples_buffer_a
[i
];
921 ((int16_t*)outbuffer
)[i
* alac
->numchannels
] = sample
;
928 for (i
= 0; i
< outputsamples
; i
++)
930 int32_t sample
= alac
->outputsamples_buffer_a
[i
];
932 if (uncompressed_bytes
)
935 sample
= sample
<< (uncompressed_bytes
* 8);
936 mask
= ~(0xFFFFFFFF << (uncompressed_bytes
* 8));
937 sample
|= alac
->uncompressed_bytes_buffer_a
[i
] & mask
;
940 ((uint8_t*)outbuffer
)[i
* alac
->numchannels
* 3] = (sample
) & 0xFF;
941 ((uint8_t*)outbuffer
)[i
* alac
->numchannels
* 3 + 1] = (sample
>> 8) & 0xFF;
942 ((uint8_t*)outbuffer
)[i
* alac
->numchannels
* 3 + 2] = (sample
>> 16) & 0xFF;
948 fprintf(stderr
, "FIXME: unimplemented sample size %i\n", alac
->setinfo_sample_size
);
955 case 1: /* 2 channels */
961 int uncompressed_bytes
;
963 uint8_t interlacing_shift
;
964 uint8_t interlacing_leftweight
;
966 /* 2^result = something to do with output waiting.
967 * perhaps matters if we read > 1 frame in a pass?
971 readbits(alac
, 12); /* unknown, skip 12 bits */
973 hassize
= readbits(alac
, 1); /* the output sample size is stored soon */
975 uncompressed_bytes
= readbits(alac
, 2); /* the number of bytes in the (compressed) stream that are not compressed */
977 isnotcompressed
= readbits(alac
, 1); /* whether the frame is compressed */
981 /* now read the number of samples,
982 * as a 32bit integer */
983 outputsamples
= readbits(alac
, 32);
984 *outputsize
= outputsamples
* alac
->bytespersample
;
987 readsamplesize
= alac
->setinfo_sample_size
- (uncompressed_bytes
* 8) + 1;
989 if (!isnotcompressed
)
991 int16_t predictor_coef_table_a
[32];
992 int predictor_coef_num_a
;
993 int prediction_type_a
;
994 int prediction_quantitization_a
;
997 int16_t predictor_coef_table_b
[32];
998 int predictor_coef_num_b
;
999 int prediction_type_b
;
1000 int prediction_quantitization_b
;
1005 interlacing_shift
= readbits(alac
, 8);
1006 interlacing_leftweight
= readbits(alac
, 8);
1008 /******** channel 1 ***********/
1009 prediction_type_a
= readbits(alac
, 4);
1010 prediction_quantitization_a
= readbits(alac
, 4);
1012 ricemodifier_a
= readbits(alac
, 3);
1013 predictor_coef_num_a
= readbits(alac
, 5);
1015 /* read the predictor table */
1016 for (i
= 0; i
< predictor_coef_num_a
; i
++)
1018 predictor_coef_table_a
[i
] = (int16_t)readbits(alac
, 16);
1021 /******** channel 2 *********/
1022 prediction_type_b
= readbits(alac
, 4);
1023 prediction_quantitization_b
= readbits(alac
, 4);
1025 ricemodifier_b
= readbits(alac
, 3);
1026 predictor_coef_num_b
= readbits(alac
, 5);
1028 /* read the predictor table */
1029 for (i
= 0; i
< predictor_coef_num_b
; i
++)
1031 predictor_coef_table_b
[i
] = (int16_t)readbits(alac
, 16);
1034 /*********************/
1035 if (uncompressed_bytes
)
1036 { /* see mono case */
1038 for (i
= 0; i
< outputsamples
; i
++)
1040 alac
->uncompressed_bytes_buffer_a
[i
] = readbits(alac
, uncompressed_bytes
* 8);
1041 alac
->uncompressed_bytes_buffer_b
[i
] = readbits(alac
, uncompressed_bytes
* 8);
1046 entropy_rice_decode(alac
,
1047 alac
->predicterror_buffer_a
,
1050 alac
->setinfo_rice_initialhistory
,
1051 alac
->setinfo_rice_kmodifier
,
1052 ricemodifier_a
* alac
->setinfo_rice_historymult
/ 4,
1053 (1 << alac
->setinfo_rice_kmodifier
) - 1);
1055 if (prediction_type_a
== 0)
1056 { /* adaptive fir */
1057 predictor_decompress_fir_adapt(alac
->predicterror_buffer_a
,
1058 alac
->outputsamples_buffer_a
,
1061 predictor_coef_table_a
,
1062 predictor_coef_num_a
,
1063 prediction_quantitization_a
);
1066 { /* see mono case */
1067 fprintf(stderr
, "FIXME: unhandled predicition type: %i\n", prediction_type_a
);
1071 entropy_rice_decode(alac
,
1072 alac
->predicterror_buffer_b
,
1075 alac
->setinfo_rice_initialhistory
,
1076 alac
->setinfo_rice_kmodifier
,
1077 ricemodifier_b
* alac
->setinfo_rice_historymult
/ 4,
1078 (1 << alac
->setinfo_rice_kmodifier
) - 1);
1080 if (prediction_type_b
== 0)
1081 { /* adaptive fir */
1082 predictor_decompress_fir_adapt(alac
->predicterror_buffer_b
,
1083 alac
->outputsamples_buffer_b
,
1086 predictor_coef_table_b
,
1087 predictor_coef_num_b
,
1088 prediction_quantitization_b
);
1092 fprintf(stderr
, "FIXME: unhandled predicition type: %i\n", prediction_type_b
);
1096 { /* not compressed, easy case */
1097 if (alac
->setinfo_sample_size
<= 16)
1100 for (i
= 0; i
< outputsamples
; i
++)
1102 int32_t audiobits_a
, audiobits_b
;
1104 audiobits_a
= readbits(alac
, alac
->setinfo_sample_size
);
1105 audiobits_b
= readbits(alac
, alac
->setinfo_sample_size
);
1107 audiobits_a
= SIGN_EXTENDED32(audiobits_a
, alac
->setinfo_sample_size
);
1108 audiobits_b
= SIGN_EXTENDED32(audiobits_b
, alac
->setinfo_sample_size
);
1110 alac
->outputsamples_buffer_a
[i
] = audiobits_a
;
1111 alac
->outputsamples_buffer_b
[i
] = audiobits_b
;
1117 for (i
= 0; i
< outputsamples
; i
++)
1119 int32_t audiobits_a
, audiobits_b
;
1121 audiobits_a
= readbits(alac
, 16);
1122 audiobits_a
= audiobits_a
<< (alac
->setinfo_sample_size
- 16);
1123 audiobits_a
|= readbits(alac
, alac
->setinfo_sample_size
- 16);
1124 audiobits_a
= SignExtend24(audiobits_a
);
1126 audiobits_b
= readbits(alac
, 16);
1127 audiobits_b
= audiobits_b
<< (alac
->setinfo_sample_size
- 16);
1128 audiobits_b
|= readbits(alac
, alac
->setinfo_sample_size
- 16);
1129 audiobits_b
= SignExtend24(audiobits_b
);
1131 alac
->outputsamples_buffer_a
[i
] = audiobits_a
;
1132 alac
->outputsamples_buffer_b
[i
] = audiobits_b
;
1135 uncompressed_bytes
= 0; // always 0 for uncompressed
1136 interlacing_shift
= 0;
1137 interlacing_leftweight
= 0;
1140 switch(alac
->setinfo_sample_size
)
1144 deinterlace_16(alac
->outputsamples_buffer_a
,
1145 alac
->outputsamples_buffer_b
,
1146 (int16_t*)outbuffer
,
1150 interlacing_leftweight
);
1155 deinterlace_24(alac
->outputsamples_buffer_a
,
1156 alac
->outputsamples_buffer_b
,
1158 alac
->uncompressed_bytes_buffer_a
,
1159 alac
->uncompressed_bytes_buffer_b
,
1160 (int16_t*)outbuffer
,
1164 interlacing_leftweight
);
1169 fprintf(stderr
, "FIXME: unimplemented sample size %i\n", alac
->setinfo_sample_size
);
1180 alac_file
*create_alac(int samplesize
, int numchannels
)
1182 alac_file
*newfile
= malloc(sizeof(alac_file
));
1184 newfile
->samplesize
= samplesize
;
1185 newfile
->numchannels
= numchannels
;
1186 newfile
->bytespersample
= (samplesize
/ 8) * numchannels
;
1191 void destroy_alac(alac_file
*alac
)
1194 deallocate_buffers(alac
);